-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
RedirectITest.java
130 lines (119 loc) · 4.61 KB
/
RedirectITest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.restassured.itest.java;
import io.restassured.RestAssured;
import io.restassured.config.HttpClientConfig;
import io.restassured.config.RedirectConfig;
import io.restassured.config.RestAssuredConfig;
import io.restassured.itest.java.support.WithJetty;
import org.apache.http.client.ClientProtocolException;
import org.junit.Test;
import static io.restassured.RestAssured.given;
import static org.apache.http.client.params.ClientPNames.COOKIE_POLICY;
import static org.apache.http.client.params.CookiePolicy.BROWSER_COMPATIBILITY;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
public class RedirectITest extends WithJetty {
@Test
public void followsRedirectsByDefault() throws Exception {
given().
param("url", "/hello").
expect().
body("hello", equalTo("Hello Scalatra")).
when().
get("/redirect");
}
@Test
public void doesntFollowRedirectsIfExplicitlySpecified() throws Exception {
given().
redirects().follow(false).
param("url", "/hello").
expect().
statusCode(302).
header("Location", is("http://localhost:8080/hello")).
when().
get("/redirect");
}
@Test
public void doesntFollowRedirectsIfSpecifiedStaticallyInRedirectConfig() throws Exception {
RestAssured.config = RestAssuredConfig.newConfig().redirect(RedirectConfig.redirectConfig().followRedirects(false));
try {
given().
param("url", "/hello").
expect().
statusCode(302).
header("Location", is("http://localhost:8080/hello")).
when().
get("/redirect");
} finally {
RestAssured.reset();
}
}
@Test(expected = ClientProtocolException.class)
public void throwsClientProtocolExceptionIfMaxNumberOfRedirectAreExceeded() throws Exception {
RestAssured.config = RestAssuredConfig.config().redirect(RedirectConfig.redirectConfig().followRedirects(true).and().maxRedirects(0));
try {
given().
param("url", "/hello").
expect().
statusCode(302).
header("Location", is("http://localhost:8080/hello")).
when().
get("/redirect");
} finally {
RestAssured.reset();
}
}
@Test
public void definingRedirectConfigInTheDSLOverridesSettingsFromDefaultConfig() throws Exception {
RestAssured.config = RestAssuredConfig.config().redirect(RedirectConfig.redirectConfig().followRedirects(false).and().maxRedirects(0));
try {
given().
redirects().follow(true).and().redirects().max(1).
param("url", "/hello").
expect().
statusCode(200).
body("hello", equalTo("Hello Scalatra")).
when().
get("/redirect");
} finally {
RestAssured.reset();
}
}
@Test
public void cookiesAreReceivedOnWhenServerReturns302() throws Exception {
given().
redirects().follow(false).
param("url", "/hello").
expect().
statusCode(302).
cookie("cookieName", "cookieValue").
header("location", "http://localhost:8080/hello").
when().
get("/redirect-and-set-cookie");
}
@Test
public void cookiesAreIncludedInRedirectsWhenCookiePolicyIsBrowserCompatibility() throws Exception {
given().
config(RestAssuredConfig.newConfig().httpClient(HttpClientConfig.httpClientConfig().setParam(COOKIE_POLICY, BROWSER_COMPATIBILITY))).
param("url", "/reflect").
expect().
statusCode(200).
cookie("cookieName", "cookieValue").
when().
get("/redirect-and-set-cookie");
}
}