Skip to content

Commit 179fc6c

Browse files
Add headers to response and token scenario for auth tests (#156)
* Add headers to http response and add token scenario for auth tests
1 parent 923d058 commit 179fc6c

File tree

6 files changed

+96
-34
lines changed

6 files changed

+96
-34
lines changed

src/functionaltests/java/com/ericsson/ei/rules/RuleCheckSteps.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public void get_request_from_REST_API_return_response_code_of_and_status_as(Stri
111111
HttpRequest getRequest = new HttpRequest(HttpMethod.GET);
112112
ResponseEntity<String> apiResponse = getRequest.setPort(applicationPort)
113113
.setHost(hostName)
114-
.setHeaders("content-type", "application/json")
115-
.setHeaders("Accept", "application/json")
114+
.addHeader("content-type", "application/json")
115+
.addHeader("Accept", "application/json")
116116
.setEndpoint(endpoint)
117117
.performRequest();
118118

src/functionaltests/java/com/ericsson/ei/subscriptions/authentication/AuthenticationSteps.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.ericsson.ei.controller.model.GetSubscriptionResponse;
44
import com.ericsson.ei.utils.FunctionalTestBase;
5+
import com.ericsson.ei.utils.HttpExecutor;
56
import com.ericsson.ei.utils.HttpRequest;
67
import com.ericsson.ei.utils.HttpRequest.HttpMethod;
78
import com.ericsson.ei.utils.TestLDAPInitializer;
@@ -30,14 +31,16 @@ public class AuthenticationSteps extends FunctionalTestBase {
3031

3132
private static final String SUBSCRIPTION = "src/functionaltests/resources/subscription_single.json";
3233
private static final String SUBSCRIPTION_NAME = "Subscription_Test";
34+
private static final String X_AUTH_TOKEN = "X-Auth-Token";
3335

3436
@LocalServerPort
3537
private int applicationPort;
3638
private String hostName = getHostName();
3739
private HttpRequest httpRequest;
3840
private ResponseEntity<String> response;
41+
private String token;
3942

40-
@Before("@RESTWithCredentials,@RESTWithSession")
43+
@Before("@RESTWithCredentials,@RESTWithSessionCookie")
4144
public void beforeScenario() throws Throwable {
4245
httpRequest = new HttpRequest(HttpMethod.GET);
4346
httpRequest.setHost(hostName).setPort(applicationPort).setEndpoint("/auth/logout");
@@ -48,6 +51,11 @@ public void beforeScenario() throws Throwable {
4851
httpRequest.performRequest();
4952
}
5053

54+
@Before("@RESTWithTokenId")
55+
public void beforeScenarioSecond() {
56+
client_is_replaced();
57+
}
58+
5159
@Given("^LDAP is activated$")
5260
public void ldap_is_activated() throws Throwable {
5361
String expectedContent = new JSONObject().put("security", true).toString();
@@ -59,7 +67,7 @@ public void ldap_is_activated() throws Throwable {
5967
assertEquals(expectedContent, response.getBody().toString());
6068
}
6169

62-
@When("^a (\\w+) request is prepared for REST API \"(.*)\"")
70+
@When("^a (\\w+) request is prepared for REST API \"(.*)\"$")
6371
public void request_to_rest_api(String method, String endpoint) throws Throwable {
6472
switch (method) {
6573
case "POST":
@@ -88,7 +96,12 @@ public void request_sent() throws Throwable {
8896
response = httpRequest.performRequest();
8997
}
9098

91-
@Then("^response code (\\d+) is received")
99+
@When("^authentication token is attached$")
100+
public void auth_token_attached() throws Throwable {
101+
httpRequest.addHeader(X_AUTH_TOKEN, token);
102+
}
103+
104+
@Then("^response code (\\d+) is received$")
92105
public void get_response_code(int statusCode) throws Throwable {
93106
assertEquals(HttpStatus.valueOf(statusCode), response.getStatusCode());
94107
}
@@ -111,4 +124,14 @@ public void subscription_with_name_created(String check) throws Throwable {
111124
assertEquals(SUBSCRIPTION_NAME, subscription.getFoundSubscriptions().get(0).getSubscriptionName());
112125
}
113126
}
127+
128+
@Then("^authentication token is saved$")
129+
public void auth_token_saved() {
130+
token = response.getHeaders().getFirst(X_AUTH_TOKEN);
131+
}
132+
133+
@Then("^client is replaced$")
134+
public void client_is_replaced() {
135+
HttpExecutor.getInstance().recreateHttpClient();
136+
}
114137
}

src/functionaltests/java/com/ericsson/ei/subscriptions/bulk/SubscriptionBulkSteps.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public void make_a_POST_request_with_list_of_subscriptions_to_the_subscription_R
4747
response = postRequest.setHost(hostName)
4848
.setPort(port)
4949
.setEndpoint(endpoint)
50-
.setHeaders("content-type", "application/json")
51-
.setHeaders("Accept", "application/json")
50+
.addHeader("content-type", "application/json")
51+
.addHeader("Accept", "application/json")
5252
.setBody(subscriptions.toString())
5353
.performRequest();
5454
}
@@ -61,7 +61,7 @@ public void make_a_GET_request_with_list_of_subscriptions_names_to_the_subscript
6161
response = getRequest.setHost(hostName)
6262
.setPort(port)
6363
.setEndpoint(endpoint + "/" + subscriptionsNamesList)
64-
.setHeaders("Accept", "application/json")
64+
.addHeader("Accept", "application/json")
6565
.performRequest();
6666
}
6767

@@ -73,7 +73,7 @@ public void make_a_DELETE_request_with_list_of_subscriptions_names_to_the_subscr
7373
response = deleteRequest.setHost(hostName)
7474
.setPort(port)
7575
.setEndpoint(endpoint + "/" + subscriptionsNamesList)
76-
.setHeaders("Accept", "application/json")
76+
.addHeader("Accept", "application/json")
7777
.performRequest();
7878
}
7979

@@ -85,8 +85,8 @@ public void make_a_PUT_request_with_list_of_subscriptions_to_the_subscription_RE
8585
response = putRequest.setHost(hostName)
8686
.setPort(port)
8787
.setEndpoint(endpoint)
88-
.setHeaders("content-type", "application/json")
89-
.setHeaders("Accept", "application/json")
88+
.addHeader("content-type", "application/json")
89+
.addHeader("Accept", "application/json")
9090
.setBody(subscriptions.toString())
9191
.performRequest();
9292
}
@@ -123,7 +123,7 @@ public void number_of_retrieved_subscriptions_using_REST_API_is(
123123
response = getRequest.setHost(hostName)
124124
.setPort(port)
125125
.setEndpoint(endpoint)
126-
.setHeaders("Accept", "application/json")
126+
.addHeader("Accept", "application/json")
127127
.performRequest();
128128
retrievedSubscriptions = new JSONArray(response.getBody().toString());
129129
assertEquals(subscriptionsNumber, retrievedSubscriptions.length());

src/functionaltests/java/com/ericsson/ei/subscriptions/repeatHandler/SubscriptionRepeatHandlerSteps.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ public void i_make_a_DELETE_request_with_subscription_name_to_the_subscription_R
117117
HttpRequest deleteRequest = new HttpRequest(HttpRequest.HttpMethod.DELETE);
118118
ResponseEntity response = deleteRequest.setHost(getHostName())
119119
.setPort(applicationPort)
120-
.setHeaders("content-type", "application/json")
121-
.setHeaders("Accept", "application/json")
120+
.addHeader("content-type", "application/json")
121+
.addHeader("Accept", "application/json")
122122
.setEndpoint(subscriptionEndPoint + name)
123123
.performRequest();
124124
assertEquals(HttpStatus.OK.value(), response.getStatusCodeValue());
Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.ericsson.ei.utils;
22

3-
import org.apache.commons.lang3.StringUtils;
3+
import org.apache.http.Header;
44
import org.apache.http.client.methods.CloseableHttpResponse;
55
import org.apache.http.client.methods.HttpRequestBase;
66
import org.apache.http.impl.client.CloseableHttpClient;
@@ -10,47 +10,69 @@
1010
import org.slf4j.LoggerFactory;
1111
import org.springframework.http.HttpStatus;
1212
import org.springframework.http.ResponseEntity;
13+
import org.springframework.util.LinkedMultiValueMap;
14+
import org.springframework.util.MultiValueMap;
1315

1416
import java.io.IOException;
1517

16-
17-
public class HttpExecutor {
18+
public final class HttpExecutor {
1819

1920
private static HttpExecutor instance;
2021
private CloseableHttpClient client = HttpClientBuilder.create().build();
2122
private static final Logger LOGGER = LoggerFactory.getLogger(HttpExecutor.class);
2223

23-
private HttpExecutor() {
24-
25-
}
26-
2724
public static HttpExecutor getInstance() {
28-
if(instance == null) {
25+
if (instance == null) {
2926
instance = new HttpExecutor();
3027
}
31-
28+
3229
return instance;
3330
}
31+
32+
/**
33+
* Close existing HttpClient and create a new one.
34+
*
35+
* @throws IOException
36+
*/
37+
public void recreateHttpClient() {
38+
if (this.client != null) {
39+
try {
40+
this.client.close();
41+
} catch (IOException e) {
42+
LOGGER.error("Error: {}", e.getMessage());
43+
}
44+
}
45+
this.client = HttpClientBuilder.create().build();
46+
}
47+
3448
/**
3549
* Handle the response from a HTTP request
50+
*
3651
* @param request
37-
* A HTTP request method, e.g. httpGet, httpPost
38-
* @return ResponseEntity
39-
* containing the json content of the http response and status code from request
40-
* */
52+
* A HTTP request method, e.g. httpGet, httpPost
53+
* @return ResponseEntity containing the resulting body, headers and status
54+
* code from request
55+
*/
4156
public ResponseEntity<String> executeRequest(HttpRequestBase request) {
4257
int statusCode = HttpStatus.PROCESSING.value();
4358
String jsonContent = "";
59+
Header[] headers = null;
4460

45-
try(CloseableHttpResponse httpResponse = client.execute(request)) {
46-
if(httpResponse.getEntity() != null) {
47-
jsonContent = StringUtils.defaultIfBlank(EntityUtils.toString(httpResponse.getEntity(), "utf-8"), "");
61+
try (CloseableHttpResponse httpResponse = client.execute(request)) {
62+
if (httpResponse.getEntity() != null) {
63+
jsonContent = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
4864
}
4965
statusCode = httpResponse.getStatusLine().getStatusCode();
50-
} catch(IOException e) {
66+
headers = httpResponse.getAllHeaders();
67+
} catch (IOException e) {
5168
LOGGER.error(e.getMessage(), e);
5269
}
5370

54-
return new ResponseEntity<>(jsonContent, HttpStatus.valueOf(statusCode));
71+
MultiValueMap<String, String> headersMap = new LinkedMultiValueMap<String, String>();
72+
for (Header header : headers) {
73+
headersMap.add(header.getName(), header.getValue());
74+
}
75+
76+
return new ResponseEntity<>(jsonContent, headersMap, HttpStatus.valueOf(statusCode));
5577
}
5678
}

src/functionaltests/resources/features/authentication.feature

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Author: valentin.tyhonov@ericsson.com
1+
#Author: valentin.tyhonov@ericsson.com, christoffer.cortes.sjowall@ericsson.com
22
#Keywords Summary :
33
#Feature: List of scenarios.
44
#Scenario: Business rule through list of steps with arguments.
@@ -35,7 +35,7 @@ Feature: Test Authentication
3535
Then response code 200 is received
3636
And subscription is created
3737

38-
@RESTWithSession
38+
@RESTWithSessionCookie
3939
Scenario: Call an REST API with session credentials
4040
Given LDAP is activated
4141
When a GET request is prepared for REST API "/auth/login"
@@ -47,4 +47,21 @@ Feature: Test Authentication
4747
Then response code 200 is received
4848
When a GET request is prepared for REST API "/auth/login"
4949
And request is sent
50+
Then response code 200 is received
51+
52+
@RESTWithTokenId
53+
Scenario: Call an REST API with session credentials
54+
Given LDAP is activated
55+
When a GET request is prepared for REST API "/auth/login"
56+
And request is sent
57+
Then response code 401 is received
58+
When a GET request is prepared for REST API "/auth/login"
59+
And username "gauss" and password "password" is used as credentials
60+
And request is sent
61+
Then response code 200 is received
62+
And authentication token is saved
63+
And client is replaced
64+
When a GET request is prepared for REST API "/auth/login"
65+
And authentication token is attached
66+
And request is sent
5067
Then response code 200 is received

0 commit comments

Comments
 (0)