001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.fcrepo.client; 019 020import static java.net.URI.create; 021import static org.fcrepo.client.TestUtils.RDF_XML; 022import static org.fcrepo.client.TestUtils.baseUrl; 023import static org.fcrepo.client.TestUtils.rdfXml; 024import static org.fcrepo.client.TestUtils.setField; 025import static org.junit.Assert.assertEquals; 026import static org.mockito.Matchers.any; 027import static org.mockito.Mockito.when; 028 029import java.io.IOException; 030import java.net.URI; 031 032import org.apache.commons.io.IOUtils; 033import org.apache.http.Header; 034import org.apache.http.StatusLine; 035import org.apache.http.client.methods.CloseableHttpResponse; 036import org.apache.http.client.methods.HttpUriRequest; 037import org.apache.http.entity.ByteArrayEntity; 038import org.apache.http.impl.client.CloseableHttpClient; 039import org.apache.http.message.BasicHeader; 040import org.junit.Test; 041import org.junit.runner.RunWith; 042import org.mockito.Mock; 043import org.mockito.runners.MockitoJUnitRunner; 044 045/** 046 * @author acoburn 047 */ 048@RunWith(MockitoJUnitRunner.class) 049public class FcrepoClientAuthTest { 050 051 private FcrepoClient testClient; 052 053 @Mock 054 private CloseableHttpClient mockHttpclient; 055 056 @Mock 057 private CloseableHttpResponse mockResponse; 058 059 @Mock 060 private StatusLine mockStatus; 061 062 @Test 063 public void testAuthNoHost() throws IOException, FcrepoOperationFailedException { 064 final int status = 200; 065 final URI uri = create(baseUrl); 066 final ByteArrayEntity entity = new ByteArrayEntity(rdfXml.getBytes()); 067 068 testClient = FcrepoClient.client() 069 .credentials("user", "pass") 070 .throwExceptionOnFailure() 071 .build(); 072 setField(testClient, "httpclient", mockHttpclient); 073 entity.setContentType(RDF_XML); 074 doSetupMockRequest(RDF_XML, entity, status); 075 076 final FcrepoResponse response = testClient.get(uri) 077 .accept(RDF_XML) 078 .perform(); 079 080 assertEquals(response.getUrl(), uri); 081 assertEquals(response.getStatusCode(), status); 082 assertEquals(response.getContentType(), RDF_XML); 083 assertEquals(response.getLocation(), null); 084 assertEquals(IOUtils.toString(response.getBody()), rdfXml); 085 } 086 087 @Test 088 public void testAuthWithHost() throws IOException, FcrepoOperationFailedException { 089 final int status = 200; 090 final URI uri = create(baseUrl); 091 final ByteArrayEntity entity = new ByteArrayEntity(rdfXml.getBytes()); 092 093 testClient = FcrepoClient.client() 094 .credentials("user", "pass") 095 .authScope("localhost") 096 .throwExceptionOnFailure() 097 .build(); 098 setField(testClient, "httpclient", mockHttpclient); 099 entity.setContentType(RDF_XML); 100 doSetupMockRequest(RDF_XML, entity, status); 101 102 final FcrepoResponse response = testClient.get(uri) 103 .accept(RDF_XML) 104 .perform(); 105 106 assertEquals(response.getUrl(), uri); 107 assertEquals(response.getStatusCode(), status); 108 assertEquals(response.getContentType(), RDF_XML); 109 assertEquals(response.getLocation(), null); 110 assertEquals(IOUtils.toString(response.getBody()), rdfXml); 111 } 112 113 @Test 114 public void testAuthNoPassword() throws IOException, FcrepoOperationFailedException { 115 final int status = 200; 116 final URI uri = create(baseUrl); 117 final ByteArrayEntity entity = new ByteArrayEntity(rdfXml.getBytes()); 118 119 testClient = FcrepoClient.client() 120 .credentials("user", null) 121 .throwExceptionOnFailure() 122 .build(); 123 setField(testClient, "httpclient", mockHttpclient); 124 entity.setContentType(RDF_XML); 125 doSetupMockRequest(RDF_XML, entity, status); 126 127 final FcrepoResponse response = testClient.get(uri) 128 .accept(RDF_XML) 129 .perform(); 130 131 assertEquals(response.getUrl(), uri); 132 assertEquals(response.getStatusCode(), status); 133 assertEquals(response.getContentType(), RDF_XML); 134 assertEquals(response.getLocation(), null); 135 assertEquals(IOUtils.toString(response.getBody()), rdfXml); 136 } 137 138 private void doSetupMockRequest(final String contentType, final ByteArrayEntity entity, final int status) 139 throws IOException { 140 final Header contentTypeHeader = new BasicHeader("Content-Type", contentType); 141 final Header[] headers = new Header[] { contentTypeHeader }; 142 143 when(mockHttpclient.execute(any(HttpUriRequest.class))).thenReturn(mockResponse); 144 when(mockResponse.getAllHeaders()).thenReturn(headers); 145 when(mockResponse.getEntity()).thenReturn(entity); 146 when(mockResponse.getStatusLine()).thenReturn(mockStatus); 147 when(mockStatus.getStatusCode()).thenReturn(status); 148 } 149}