Skip to content

Commit

Permalink
Add tests for HTTPRoute filters
Browse files Browse the repository at this point in the history
  • Loading branch information
sgayangi committed Jun 20, 2024
1 parent 0ae198f commit 76249c8
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 23 deletions.
62 changes: 44 additions & 18 deletions runtime/config-deployer-service/ballerina/APIClient.bal
Original file line number Diff line number Diff line change
Expand Up @@ -860,30 +860,27 @@ public class APIClient {
model:HTTPHeader[] setHeaders = [];
string[] removeHeaders = [];
boolean hasRedirectPolicy = false;
model:HTTPRouteFilter headerModifierFilter = {'type: "RequestHeaderModifier"};
if !isRequest {
headerModifierFilter.'type = "ResponseHeaderModifier";
}

foreach APKOperationPolicy policy in operationPolicies {
if policy is HeaderModifierPolicy {
HeaderModifierPolicyParameters policyParameters = policy.parameters;
match policy.policyName {
AddHeaders => {
ModifierHeader[] headers = <ModifierHeader[]>policyParameters.headers;
foreach ModifierHeader header in headers {
headers.push(header);
addHeaders.push(header);
}
}
SetHeaders => {
ModifierHeader[] headers = <ModifierHeader[]>policyParameters.headers;
foreach ModifierHeader header in headers {
headers.push(header);
setHeaders.push(header);
}
}
RemoveHeaders => {
string[] headers = <string[]>policyParameters.headers;
foreach string header in headers {
headers.push(header);
removeHeaders.push(header);
}
}
}
Expand Down Expand Up @@ -956,18 +953,47 @@ public class APIClient {
httpRouteFilters.push(redirectFilter);
}
}
if isRequest {
model:HTTPHeaderFilter requestHeaderModifier = {};
if addHeaders != [] {
requestHeaderModifier.add = addHeaders;
}
if setHeaders != [] {
requestHeaderModifier.set = setHeaders;
}
if removeHeaders != [] {
requestHeaderModifier.remove = removeHeaders;
}
log:printInfo(addHeaders.length().toString());
log:printInfo(setHeaders.length().toString());
log:printInfo(removeHeaders.length().toString());

if addHeaders != [] {
headerModifierFilter.requestHeaderModifier.add = addHeaders;
}
if setHeaders != [] {
headerModifierFilter.requestHeaderModifier.set = setHeaders;
}
if removeHeaders != [] {
headerModifierFilter.requestHeaderModifier.remove = removeHeaders;
}
if addHeaders.length() > 0 || setHeaders.length() > 0 || removeHeaders.length() > 0 {
httpRouteFilters.push(headerModifierFilter);
if addHeaders.length() > 0 || setHeaders.length() > 0 || removeHeaders.length() > 0 {

model:HTTPRouteFilter headerModifierFilter = {
'type: "RequestHeaderModifier",
requestHeaderModifier: requestHeaderModifier
};
httpRouteFilters.push(headerModifierFilter);
}
} else {
model:HTTPHeaderFilter responseHeaderModifier = {};
if addHeaders != [] {
responseHeaderModifier.add = addHeaders;
}
if setHeaders != [] {
responseHeaderModifier.set = setHeaders;
}
if removeHeaders != [] {
responseHeaderModifier.remove = removeHeaders;
}
if addHeaders.length() > 0 || setHeaders.length() > 0 || removeHeaders.length() > 0 {
model:HTTPRouteFilter headerModifierFilter = {
'type: "ResponseHeaderModifier",
responseHeaderModifier: responseHeaderModifier
};
httpRouteFilters.push(headerModifierFilter);
}
}

return [httpRouteFilters, hasRedirectPolicy];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.net.ssl.TrustManager;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
Expand Down Expand Up @@ -87,7 +88,11 @@ public SimpleHTTPClient() throws NoSuchAlgorithmException, KeyStoreException, Ke
.build();

final SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslcontext);
RequestConfig requestConfig = RequestConfig.custom()
.setRedirectsEnabled(false) // Disable redirects
.build();
this.client = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setSSLSocketFactory(csf)
.evictExpiredConnections()
.setMaxConnPerRoute(100)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
id: "api-with-request-and-response-filters"
id: "api-with-header-modifier-filters"
name: "EmployeeServiceAPI"
basePath: "/request-and-response-filters"
basePath: "/header-modifier-filters"
version: "3.14"
type: "REST"
defaultVersion: false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
id: "api-with-request-redirect-filter"
name: "EmployeeServiceAPI"
basePath: "/request-redirect-filter"
version: "3.14"
type: "REST"
defaultVersion: false
endpointConfigurations:
production:
endpoint: "http:https://backend:80/anything"
operations:
- target: "/employee"
verb: "GET"
secured: false
scopes: []
operationPolicies:
request:
- policyName: RequestRedirect
policyVersion: v1
parameters:
url: "http:https://backend:80/anything"
statusCode: 301
- target: "/employee"
verb: "POST"
secured: true
scopes: []
- target: "/employee/{employeeId}"
verb: "PUT"
secured: true
scopes: []
- target: "/employee/{employeeId}"
verb: "DELETE"
secured: true
scopes: []
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ Feature: Test HTTPRoute Filter Header Modifier functionality
Scenario: Test request and response header modification functionality
Given The system is ready
And I have a valid subscription
When I use the APK Conf file "artifacts/apk-confs/request_and_response_filters.apk-conf"
When I use the APK Conf file "artifacts/apk-confs/header-modifier-filter.apk-conf"
And the definition file "artifacts/definitions/employees_api.json"
And make the API deployment request
Then the response status code should be 200
Then I set headers
| Authorization | bearer ${accessToken} |
And I send "GET" request to "https://default.gw.wso2.com:9095/request-and-response-filters/3.14/employee/" with body ""
And I send "GET" request to "https://default.gw.wso2.com:9095/header-modifier-filters/3.14/employee/" with body ""
And I eventually receive 200 response code, not accepting
| 401 |
And the response body should contain "\"Test-Request-Header\": \"Test-Value\""
Expand All @@ -22,7 +22,7 @@ Feature: Test HTTPRoute Filter Header Modifier functionality
Scenario: Undeploy the API
Given The system is ready
And I have a valid subscription
When I undeploy the API whose ID is "api-with-request-and-response-filters"
When I undeploy the API whose ID is "api-with-header-modifier-filters"
Then the response status code should be 202


Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Feature: Test HTTPRoute Filter Request Redirect functionality
Scenario: Test request redirect functionality
Given The system is ready
And I have a valid subscription
When I use the APK Conf file "artifacts/apk-confs/request-redirect-filter.apk-conf"
And the definition file "artifacts/definitions/employees_api.json"
And make the API deployment request
Then the response status code should be 200
Then I set headers
| Authorization | bearer ${accessToken} |
And I send "GET" request to "https://default.gw.wso2.com:9095/request-redirect-filter/3.14/employee/" with body ""
And I eventually receive 301 response code, not accepting
| 401 |

Scenario: Undeploy the API
Given The system is ready
And I have a valid subscription
When I undeploy the API whose ID is "api-with-request-redirect-filter"
Then the response status code should be 202


0 comments on commit 76249c8

Please sign in to comment.