Skip to content

Commit

Permalink
Merge pull request #123 from mdsol/tech/MCC-620147_Changes_to_work_wi…
Browse files Browse the repository at this point in the history
…th_POST_and_PATCH

[MCC-620147] : mauth-proxy changes to work with POST and PATCH
  • Loading branch information
austek authored May 28, 2020
2 parents 172b1da + 0807bac commit cf53825
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.mdsol.mauth.proxy;

import com.mdsol.mauth.Signer;

import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

class MAuthHttpRequestSigner {
Expand All @@ -21,11 +21,10 @@ class MAuthHttpRequestSigner {

void signRequest(HttpRequest request) {
final String verb = request.getMethod().name();

byte[] requestPayload = null;
String requestPayload = null;
String queryString = null;
if (request instanceof FullHttpRequest) {
requestPayload = ((FullHttpRequest) request).content().array();
requestPayload = ((FullHttpRequest)request).content().toString(StandardCharsets.UTF_8);
}
String uriString = request.getUri();
try {
Expand All @@ -37,8 +36,12 @@ void signRequest(HttpRequest request) {
}

logger.debug("Generating request headers for Verb: '" + verb + "' URI: '" + uriString +
"' Payload: " + requestPayload.toString() + "' Query parameters: " + queryString);
Map<String, String> mAuthHeaders = httpClientRequestSigner.generateRequestHeaders(verb, uriString, requestPayload, queryString);
"' Payload: " + requestPayload + "' Query parameters: " + queryString);
Map<String, String> mAuthHeaders = httpClientRequestSigner
.generateRequestHeaders(verb,
uriString,
requestPayload != null ? requestPayload.getBytes() : new byte[0],
queryString);
mAuthHeaders.forEach((key, value) -> request.headers().add(key, value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import com.github.tomakehurst.wiremock.client.WireMock._
import com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig
import com.mdsol.mauth.MAuthRequest
import com.typesafe.config.ConfigFactory
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.methods.{CloseableHttpResponse, HttpGet, HttpPatch, HttpPost}
import org.apache.http.entity.ByteArrayEntity
import org.apache.http.impl.client.HttpClients
import org.apache.http.impl.conn.DefaultProxyRoutePlanner
import org.apache.http.{HttpHost, HttpStatus}
Expand All @@ -19,6 +20,7 @@ class ProxyServerSpec extends AnyFlatSpec with Matchers with MockFactory with Be
private val MY_RESOURCE = "/my/resource"
private val QUERY_PARAMETERS = "k1=v1&k2=v2"
private val MY_RESOURCE_WITH_PARAMS = MY_RESOURCE + "?" + QUERY_PARAMETERS
private val MY_REQUEST_PAYLOAD = "{k1:v1, k2:v2}"
var service = new WireMockServer(wireMockConfig.dynamicPort)
val proxyServer = new ProxyServer(new ProxyConfig(ConfigFactory.load.resolve))

Expand Down Expand Up @@ -86,4 +88,86 @@ class ProxyServerSpec extends AnyFlatSpec with Matchers with MockFactory with Be
response.getStatusLine.getStatusCode shouldBe HttpStatus.SC_OK
}

it should "POST - correctly modify request with request payload" in {
service.stubFor(
post(urlEqualTo(MY_RESOURCE_WITH_PARAMS))
.withRequestBody(containing(""))
.withHeader(MAuthRequest.X_MWS_AUTHENTICATION_HEADER_NAME, containing("MWS "))
.withHeader(MAuthRequest.X_MWS_TIME_HEADER_NAME, matching(".*"))
.withHeader(MAuthRequest.MCC_AUTHENTICATION_HEADER_NAME, containing("MWSV2 "))
.withHeader(MAuthRequest.MCC_TIME_HEADER_NAME, matching(".*"))
.willReturn(created)
)

val response = execute(proxyServer.getPort, "post")
response.getStatusLine.getStatusCode shouldBe HttpStatus.SC_CREATED
}

it should "PATCH - correctly modify request with request payload" in {
service.stubFor(
patch(urlEqualTo(MY_RESOURCE_WITH_PARAMS))
.withRequestBody(containing(""))
.withHeader(MAuthRequest.X_MWS_AUTHENTICATION_HEADER_NAME, containing("MWS "))
.withHeader(MAuthRequest.X_MWS_TIME_HEADER_NAME, matching(".*"))
.withHeader(MAuthRequest.MCC_AUTHENTICATION_HEADER_NAME, containing("MWSV2 "))
.withHeader(MAuthRequest.MCC_TIME_HEADER_NAME, matching(".*"))
.willReturn(ok("success"))
)

val response = execute(proxyServer.getPort, "patch")
response.getStatusLine.getStatusCode shouldBe HttpStatus.SC_OK
}

it should "POST - Fail if proxy responds with 404" in {
service.stubFor(
post(urlEqualTo("/my/failure_resource?k=v"))
.withHeader(MAuthRequest.X_MWS_AUTHENTICATION_HEADER_NAME, containing("MWS "))
.withHeader(MAuthRequest.X_MWS_TIME_HEADER_NAME, matching(".*"))
.withHeader(MAuthRequest.MCC_AUTHENTICATION_HEADER_NAME, containing("MWSV2 "))
.withHeader(MAuthRequest.MCC_TIME_HEADER_NAME, matching(".*"))
.willReturn(notFound)
)

val proxy = new HttpHost("localhost", proxyServer.getPort)
val routePlanner = new DefaultProxyRoutePlanner(proxy)
val httpClient = HttpClients.custom.setRoutePlanner(routePlanner).build
val request = new HttpPost(BASE_URL + ":" + service.port + "/my/failure_resource?k=v")

val response = httpClient.execute(request)
response.getStatusLine.getStatusCode shouldBe HttpStatus.SC_NOT_FOUND
}

it should "PATCH - Fail if proxy responds with 404" in {
service.stubFor(
patch(urlEqualTo("/my/failure_resource?k=v"))
.withHeader(MAuthRequest.X_MWS_AUTHENTICATION_HEADER_NAME, containing("MWS "))
.withHeader(MAuthRequest.X_MWS_TIME_HEADER_NAME, matching(".*"))
.withHeader(MAuthRequest.MCC_AUTHENTICATION_HEADER_NAME, containing("MWSV2 "))
.withHeader(MAuthRequest.MCC_TIME_HEADER_NAME, matching(".*"))
.willReturn(notFound)
)

val proxy = new HttpHost("localhost", proxyServer.getPort)
val routePlanner = new DefaultProxyRoutePlanner(proxy)
val httpClient = HttpClients.custom.setRoutePlanner(routePlanner).build
val request = new HttpPatch(BASE_URL + ":" + service.port + "/my/failure_resource?k=v")

val response = httpClient.execute(request)
response.getStatusLine.getStatusCode shouldBe HttpStatus.SC_NOT_FOUND
}

private def execute(proxyServerPort: Int, verb: String): CloseableHttpResponse = {
val proxy = new HttpHost("localhost", proxyServerPort)
val routePlanner = new DefaultProxyRoutePlanner(proxy)
val httpClient = HttpClients.custom.setRoutePlanner(routePlanner).build
if (verb.equals("post")) {
val request = new HttpPost(BASE_URL + ":" + service.port + MY_RESOURCE_WITH_PARAMS)
request.setEntity(new ByteArrayEntity(MY_REQUEST_PAYLOAD.getBytes("UTF-8")))
httpClient.execute(request)
} else {
val request = new HttpPatch(BASE_URL + ":" + service.port + MY_RESOURCE_WITH_PARAMS)
request.setEntity(new ByteArrayEntity(MY_REQUEST_PAYLOAD.getBytes("UTF-8")))
httpClient.execute(request)
}
}
}

0 comments on commit cf53825

Please sign in to comment.