|
| 1 | +/* |
| 2 | + * Copyright 2002-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.http.client; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | + |
| 21 | +import org.junit.Rule; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.rules.ExpectedException; |
| 24 | + |
| 25 | +import org.springframework.beans.DirectFieldAccessor; |
| 26 | +import org.springframework.http.HttpMethod; |
| 27 | + |
| 28 | +import static org.junit.Assert.*; |
| 29 | +import static org.mockito.Mockito.*; |
| 30 | + |
| 31 | +/** |
| 32 | + * Tests for {@link BasicAuthorizationInterceptor}. |
| 33 | + * |
| 34 | + * @author Phillip Webb |
| 35 | + * @author Stephane Nicoll |
| 36 | + */ |
| 37 | +public class BasicAuthorizationInterceptorTests { |
| 38 | + |
| 39 | + @Rule |
| 40 | + public ExpectedException thrown = ExpectedException.none(); |
| 41 | + |
| 42 | + @Test |
| 43 | + public void createWhenUsernameIsNullShouldThrowException() { |
| 44 | + this.thrown.expect(IllegalArgumentException.class); |
| 45 | + this.thrown.expectMessage("Username must not be empty"); |
| 46 | + new BasicAuthorizationInterceptor(null, "password"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void createWhenUsernameIsEmptyShouldThrowException() throws Exception { |
| 51 | + this.thrown.expect(IllegalArgumentException.class); |
| 52 | + this.thrown.expectMessage("Username must not be empty"); |
| 53 | + new BasicAuthorizationInterceptor("", "password"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void createWhenPasswordIsNullShouldUseEmptyPassword() throws Exception { |
| 58 | + BasicAuthorizationInterceptor interceptor = new BasicAuthorizationInterceptor( |
| 59 | + "username", null); |
| 60 | + assertEquals("", new DirectFieldAccessor(interceptor).getPropertyValue("password")); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void interceptShouldAddHeader() throws Exception { |
| 65 | + SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); |
| 66 | + ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET); |
| 67 | + ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class); |
| 68 | + byte[] body = new byte[] {}; |
| 69 | + new BasicAuthorizationInterceptor("spring", "boot").intercept(request, body, |
| 70 | + execution); |
| 71 | + verify(execution).execute(request, body); |
| 72 | + assertEquals("Basic c3ByaW5nOmJvb3Q=", request.getHeaders().getFirst("Authorization")); |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments