use of ee.ria.xroad.proxy.protocol.ProxyMessageDecoder in project X-Road by nordic-institute.
the class RestMetadataServiceHandlerTest method shouldOverrideServerUrlsForYaml.
@Test
public void shouldOverrideServerUrlsForYaml() throws Exception {
RestMetadataServiceHandlerImpl handlerToTest = new RestMetadataServiceHandlerImpl();
ProxyMessageDecoder mockDecoder = mock(ProxyMessageDecoder.class);
ProxyMessageEncoder mockEncoder = mock(ProxyMessageEncoder.class);
// Test for petstore.yaml parsing
ServiceId serviceId = ServiceId.create(DEFAULT_CLIENT, GET_OPENAPI);
RestRequest mockRestRequest = mock(RestRequest.class);
when(mockRestRequest.getQuery()).thenReturn("serviceCode=yaml");
when(mockRestRequest.getServiceId()).thenReturn(serviceId);
when(mockRestRequest.getVerb()).thenReturn(RestRequest.Verb.GET);
when(mockRestRequest.getClientId()).thenReturn(DEFAULT_CLIENT);
when(mockRestRequest.getHash()).thenReturn(REQUEST_HASH);
when(mockProxyMessage.getRest()).thenReturn(mockRestRequest);
handlerToTest.startHandling(mockRequest, mockProxyMessage, mockDecoder, mockEncoder, httpClientMock, httpClientMock, mock(OpMonitoringData.class));
CachingStream yamlFileResponseBody = handlerToTest.getRestResponseBody();
String yaml = new BufferedReader(new InputStreamReader(yamlFileResponseBody.getCachedContents(), StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
assertFalse(yaml.contains("http://petstore.swagger.io/v1/cats"));
assertTrue(yaml.contains("/v1/cats"));
assertEquals(StringUtils.countMatches(yaml, "/v1/cats"), 1);
assertTrue(yaml.contains("null"));
assertEquals(StringUtils.countMatches(yaml, "null"), 2);
assertTrue(yaml.contains("- \"this\""));
assertTrue(yaml.contains("- \"should\""));
assertTrue(yaml.contains("- \"be\""));
assertTrue(yaml.contains("- \"a string\""));
assertTrue(yaml.contains("- url: \"\""));
}
use of ee.ria.xroad.proxy.protocol.ProxyMessageDecoder in project X-Road by nordic-institute.
the class RestMetadataServiceHandlerTest method shouldOverrideServerUrlsForJson.
@Test
public void shouldOverrideServerUrlsForJson() throws Exception {
RestMetadataServiceHandlerImpl handlerToTest = new RestMetadataServiceHandlerImpl();
ProxyMessageDecoder mockDecoder = mock(ProxyMessageDecoder.class);
ProxyMessageEncoder mockEncoder = mock(ProxyMessageEncoder.class);
// Test petstore.json parsing
ServiceId serviceId = ServiceId.create(SECONDARY_CLIENT, GET_OPENAPI);
RestRequest secondaryMockRestRequest = mock(RestRequest.class);
when(secondaryMockRestRequest.getQuery()).thenReturn("serviceCode=json");
when(secondaryMockRestRequest.getServiceId()).thenReturn(serviceId);
when(secondaryMockRestRequest.getVerb()).thenReturn(RestRequest.Verb.GET);
when(secondaryMockRestRequest.getClientId()).thenReturn(SECONDARY_CLIENT);
when(secondaryMockRestRequest.getHash()).thenReturn(REQUEST_HASH);
when(mockProxyMessage.getRest()).thenReturn(secondaryMockRestRequest);
handlerToTest.startHandling(mockRequest, mockProxyMessage, mockDecoder, mockEncoder, httpClientMock, httpClientMock, mock(OpMonitoringData.class));
CachingStream jsonFileResponseBody = handlerToTest.getRestResponseBody();
String json = new BufferedReader(new InputStreamReader(jsonFileResponseBody.getCachedContents(), StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
assertFalse(json.contains("https://petstore.swagger.io/v2"));
assertTrue(json.contains("\"/v2\""));
assertTrue(json.contains("https://{username}.petstore.swagger.io:{port}/{basePath}"));
}
use of ee.ria.xroad.proxy.protocol.ProxyMessageDecoder in project X-Road by nordic-institute.
the class RestMetadataServiceHandlerTest method shouldHandleGetOpenApi.
@Test
public void shouldHandleGetOpenApi() throws Exception {
RestMetadataServiceHandlerImpl handlerToTest = new RestMetadataServiceHandlerImpl();
ServiceId serviceId = ServiceId.create(DEFAULT_CLIENT, GET_OPENAPI);
when(mockRequest.getRequestURL()).thenReturn(new StringBuffer("https://securityserver:5500"));
RestRequest mockRestRequest = mock(RestRequest.class);
when(mockRestRequest.getQuery()).thenReturn("serviceCode=foobar");
when(mockRestRequest.getServiceId()).thenReturn(serviceId);
when(mockRestRequest.getVerb()).thenReturn(RestRequest.Verb.GET);
when(mockRestRequest.getClientId()).thenReturn(DEFAULT_CLIENT);
when(mockRestRequest.getHash()).thenReturn(REQUEST_HASH);
when(mockProxyMessage.getRest()).thenReturn(mockRestRequest);
ProxyMessageDecoder mockDecoder = mock(ProxyMessageDecoder.class);
ProxyMessageEncoder mockEncoder = mock(ProxyMessageEncoder.class);
handlerToTest.startHandling(mockRequest, mockProxyMessage, mockDecoder, mockEncoder, httpClientMock, httpClientMock, mock(OpMonitoringData.class));
RestResponse restResponse = handlerToTest.getRestResponse();
assertEquals(HttpStatus.SC_OK, restResponse.getResponseCode());
assertEquals("OK", restResponse.getReason());
CachingStream restResponseBody = handlerToTest.getRestResponseBody();
assertTrue(restResponseBody.getCachedContents().size() > 0);
}
use of ee.ria.xroad.proxy.protocol.ProxyMessageDecoder in project X-Road by nordic-institute.
the class ClientRestMessageProcessor method parseResponse.
private void parseResponse(HttpSender httpSender) throws Exception {
response = new ProxyMessage(httpSender.getResponseHeaders().get(HEADER_ORIGINAL_CONTENT_TYPE));
ProxyMessageDecoder decoder = new ProxyMessageDecoder(response, httpSender.getResponseContentType(), getHashAlgoId(httpSender));
try {
decoder.parse(httpSender.getResponseContent());
} catch (CodedException ex) {
throw ex.withPrefix(X_SERVICE_FAILED_X);
}
updateOpMonitoringDataByResponse(decoder);
// Ensure we have the required parts.
checkResponse();
opMonitoringData.setRestResponseStatusCode(response.getRestResponse().getResponseCode());
decoder.verify(requestServiceId.getClientId(), response.getSignature());
}
use of ee.ria.xroad.proxy.protocol.ProxyMessageDecoder in project X-Road by nordic-institute.
the class ClientMessageProcessor method parseResponse.
private void parseResponse(HttpSender httpSender) throws Exception {
log.trace("parseResponse()");
response = new ProxyMessage(httpSender.getResponseHeaders().get(HEADER_ORIGINAL_CONTENT_TYPE));
ProxyMessageDecoder decoder = new ProxyMessageDecoder(response, httpSender.getResponseContentType(), getHashAlgoId(httpSender));
try {
decoder.parse(httpSender.getResponseContent());
} catch (CodedException ex) {
throw ex.withPrefix(X_SERVICE_FAILED_X);
}
updateOpMonitoringDataByResponse(decoder);
// Ensure we have the required parts.
checkResponse();
decoder.verify(requestServiceId.getClientId(), response.getSignature());
}
Aggregations