@Override public Collection<RequestCookie> getCookies() { return this.delegate.getCookies(); }
private void writeCookies(OperationRequest request, List<String> lines) { for (RequestCookie cookie : request.getCookies()) { lines.add( String.format("'Cookie:%s=%s'", cookie.getName(), cookie.getValue())); } }
private List<Map<String, String>> getHeaders(OperationRequest request) { List<Map<String, String>> headers = new ArrayList<>(); for (Entry<String, List<String>> header : request.getHeaders().entrySet()) { for (String value : header.getValue()) { if (HttpHeaders.CONTENT_TYPE.equals(header.getKey()) && !request.getParts().isEmpty()) { headers.add(header(header.getKey(), String.format("%s; boundary=%s", value, MULTIPART_BOUNDARY))); } else { headers.add(header(header.getKey(), value)); } } } for (RequestCookie cookie : request.getCookies()) { headers.add(header(HttpHeaders.COOKIE, String.format("%s=%s", cookie.getName(), cookie.getValue()))); } if (requiresFormEncodingContentTypeHeader(request)) { headers.add(header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)); } return headers; }
@Test public void resultingRequestHasCookiesFromOriginalRequst() { List<RequestCookie> cookies = Arrays.asList(new RequestCookie("a", "alpha")); OperationRequest request = this.requestFactory.create( URI.create("http://localhost:12345"), HttpMethod.GET, new byte[0], new HttpHeaders(), new Parameters(), Collections.<OperationRequestPart>emptyList(), cookies); OperationRequest processed = this.preprocessor.preprocess(request); assertThat(processed.getCookies().size()).isEqualTo(1); }
@Test public void resultingRequestHasCookiesFromOriginalRequst() { List<RequestCookie> cookies = Arrays.asList(new RequestCookie("a", "alpha")); OperationRequest request = this.requestFactory.create( URI.create("http://localhost:12345"), HttpMethod.GET, new byte[0], new HttpHeaders(), new Parameters(), Collections.<OperationRequestPart>emptyList(), cookies); OperationRequest processed = this.preprocessor.preprocess(request); assertThat(processed.getCookies().size()).isEqualTo(1); }
@Test public void requestWithCookies() throws Exception { OperationRequest request = createOperationRequest( MockMvcRequestBuilders.get("/foo").cookie( new javax.servlet.http.Cookie("cookieName1", "cookieVal1"), new javax.servlet.http.Cookie("cookieName2", "cookieVal2"))); assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo")); assertThat(request.getMethod()).isEqualTo(HttpMethod.GET); assertThat(request.getCookies().size()).isEqualTo(2); Iterator<RequestCookie> cookieIterator = request.getCookies().iterator(); RequestCookie cookie1 = cookieIterator.next(); assertThat(cookie1.getName()).isEqualTo("cookieName1"); assertThat(cookie1.getValue()).isEqualTo("cookieVal1"); RequestCookie cookie2 = cookieIterator.next(); assertThat(cookie2.getName()).isEqualTo("cookieName2"); assertThat(cookie2.getValue()).isEqualTo("cookieVal2"); }
@Override public OperationRequest preprocess(OperationRequest request) { UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUri(request.getUri()); if (this.scheme != null) { uriBuilder.scheme(this.scheme); } if (this.host != null) { uriBuilder.host(this.host); } if (this.port != null) { if (StringUtils.hasText(this.port)) { uriBuilder.port(this.port); } else { uriBuilder.port(null); } } URI modifiedUri = uriBuilder.build(true).toUri(); HttpHeaders modifiedHeaders = modify(request.getHeaders()); modifiedHeaders.set(HttpHeaders.HOST, modifiedUri.getHost() + ((modifiedUri.getPort() != -1) ? ":" + modifiedUri.getPort() : "")); return this.contentModifyingDelegate.preprocess(new OperationRequestFactory() .create(uriBuilder.build(true).toUri(), request.getMethod(), request.getContent(), modifiedHeaders, request.getParameters(), modify(request.getParts()), request.getCookies())); }
@Test public void cookies() { RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort()) .cookie("cookie1", "cookieVal1").cookie("cookie2", "cookieVal2"); requestSpec.get("/"); OperationRequest request = this.factory .convert((FilterableRequestSpecification) requestSpec); assertThat(request.getCookies().size()).isEqualTo(2); Iterator<RequestCookie> cookieIterator = request.getCookies().iterator(); RequestCookie cookie1 = cookieIterator.next(); assertThat(cookie1.getName()).isEqualTo("cookie1"); assertThat(cookie1.getValue()).isEqualTo("cookieVal1"); RequestCookie cookie2 = cookieIterator.next(); assertThat(cookie2.getName()).isEqualTo("cookie2"); assertThat(cookie2.getValue()).isEqualTo("cookieVal2"); }
/** * Creates a new {@code OperationRequest} based on the given {@code original} but with * the given {@code newHeaders}. * @param original the original request * @param newHeaders the new headers * @return the new request with the new headers */ public OperationRequest createFrom(OperationRequest original, HttpHeaders newHeaders) { return new StandardOperationRequest(original.getUri(), original.getMethod(), original.getContent(), newHeaders, original.getParameters(), original.getParts(), original.getCookies()); }
/** * Creates a new {@code OperationRequest} based on the given {@code original} but with * the given {@code newParameters}. * @param original the original request * @param newParameters the new parameters * @return the new request with the new parameters */ public OperationRequest createFrom(OperationRequest original, Parameters newParameters) { return new StandardOperationRequest(original.getUri(), original.getMethod(), original.getContent(), original.getHeaders(), newParameters, original.getParts(), original.getCookies()); }
/** * Creates a new {@code OperationRequest} based on the given {@code original} but with * the given {@code newContent}. If the original request had a {@code Content-Length} * header it will be modified to match the length of the new content. * @param original the original request * @param newContent the new content * @return the new request with the new content */ public OperationRequest createFrom(OperationRequest original, byte[] newContent) { return new StandardOperationRequest(original.getUri(), original.getMethod(), newContent, getUpdatedHeaders(original.getHeaders(), newContent), original.getParameters(), original.getParts(), original.getCookies()); }
@Test public void requestWithCookies() throws Exception { ExchangeResult result = WebTestClient .bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> null)) .configureClient().baseUrl("http://localhost").build().get().uri("/foo") .cookie("cookieName1", "cookieVal1").cookie("cookieName2", "cookieVal2") .exchange().expectBody().returnResult(); assertThat(result.getRequestHeaders().get(HttpHeaders.COOKIE)).isNotNull(); OperationRequest request = this.converter.convert(result); assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo")); assertThat(request.getMethod()).isEqualTo(HttpMethod.GET); assertThat(request.getCookies()).hasSize(2); assertThat(request.getHeaders().get(HttpHeaders.COOKIE)).isNull(); assertThat(request.getCookies()).extracting("name").containsExactly("cookieName1", "cookieName2"); assertThat(request.getCookies()).extracting("value").containsExactly("cookieVal1", "cookieVal2"); }