WebClientOAuth2PocTests.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2002-2017 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. package webclient.oauth2.poc;
  17. import okhttp3.mockwebserver.MockResponse;
  18. import okhttp3.mockwebserver.MockWebServer;
  19. import org.junit.After;
  20. import org.junit.Before;
  21. import org.junit.Test;
  22. import org.springframework.http.HttpStatus;
  23. import org.springframework.web.reactive.function.client.ClientRequest;
  24. import org.springframework.web.reactive.function.client.ClientResponse;
  25. import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
  26. import org.springframework.web.reactive.function.client.WebClient;
  27. import reactor.core.publisher.Mono;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
  30. /**
  31. * @author Rob Winch
  32. * @since 5.0
  33. */
  34. public class WebClientOAuth2PocTests {
  35. private MockWebServer server;
  36. private WebClient webClient;
  37. @Before
  38. public void setup() {
  39. this.server = new MockWebServer();
  40. String baseUrl = this.server.url("/").toString();
  41. this.webClient = WebClient.create(baseUrl);
  42. }
  43. @After
  44. public void shutdown() throws Exception {
  45. this.server.shutdown();
  46. }
  47. @Test
  48. public void httpBasicWhenNeeded() throws Exception {
  49. this.server.enqueue(new MockResponse().setResponseCode(401).setHeader("WWW-Authenticate", "Basic realm=\"Test\""));
  50. this.server.enqueue(new MockResponse().setResponseCode(200).setBody("OK"));
  51. ClientResponse response = this.webClient
  52. .mutate()
  53. .filter(basicIfNeeded("rob", "rob"))
  54. .build()
  55. .get()
  56. .uri("/")
  57. .exchange()
  58. .block();
  59. assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
  60. assertThat(this.server.takeRequest().getHeader("Authorization")).isNull();
  61. assertThat(this.server.takeRequest().getHeader("Authorization")).isEqualTo("Basic cm9iOnJvYg==");
  62. }
  63. @Test
  64. public void httpBasicWhenNotNeeded() throws Exception {
  65. this.server.enqueue(new MockResponse().setResponseCode(200).setBody("OK"));
  66. ClientResponse response = this.webClient
  67. .mutate()
  68. .filter(basicIfNeeded("rob", "rob"))
  69. .build()
  70. .get()
  71. .uri("/")
  72. .exchange()
  73. .block();
  74. assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
  75. assertThat(this.server.getRequestCount()).isEqualTo(1);
  76. assertThat(this.server.takeRequest().getHeader("Authorization")).isNull();
  77. }
  78. private ExchangeFilterFunction basicIfNeeded(String username, String password) {
  79. return (request, next) ->
  80. next.exchange(request)
  81. .filter( r -> !HttpStatus.UNAUTHORIZED.equals(r.statusCode()))
  82. .switchIfEmpty( Mono.defer(() -> {
  83. return basicAuthentication(username, password).filter(request, next);
  84. }));
  85. }
  86. }