Search in sources :

Example 6 with ProxyMessageDecoder

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: \"\""));
}
Also used : OpMonitoringData(ee.ria.xroad.common.opmonitoring.OpMonitoringData) RestRequest(ee.ria.xroad.common.message.RestRequest) InputStreamReader(java.io.InputStreamReader) ProxyMessageDecoder(ee.ria.xroad.proxy.protocol.ProxyMessageDecoder) BufferedReader(java.io.BufferedReader) ProxyMessageEncoder(ee.ria.xroad.proxy.protocol.ProxyMessageEncoder) ServiceId(ee.ria.xroad.common.identifier.ServiceId) CachingStream(ee.ria.xroad.common.util.CachingStream) Test(org.junit.Test)

Example 7 with ProxyMessageDecoder

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}"));
}
Also used : OpMonitoringData(ee.ria.xroad.common.opmonitoring.OpMonitoringData) RestRequest(ee.ria.xroad.common.message.RestRequest) InputStreamReader(java.io.InputStreamReader) ProxyMessageDecoder(ee.ria.xroad.proxy.protocol.ProxyMessageDecoder) BufferedReader(java.io.BufferedReader) ProxyMessageEncoder(ee.ria.xroad.proxy.protocol.ProxyMessageEncoder) ServiceId(ee.ria.xroad.common.identifier.ServiceId) CachingStream(ee.ria.xroad.common.util.CachingStream) Test(org.junit.Test)

Example 8 with ProxyMessageDecoder

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);
}
Also used : OpMonitoringData(ee.ria.xroad.common.opmonitoring.OpMonitoringData) RestRequest(ee.ria.xroad.common.message.RestRequest) ProxyMessageDecoder(ee.ria.xroad.proxy.protocol.ProxyMessageDecoder) RestResponse(ee.ria.xroad.common.message.RestResponse) ProxyMessageEncoder(ee.ria.xroad.proxy.protocol.ProxyMessageEncoder) ServiceId(ee.ria.xroad.common.identifier.ServiceId) CachingStream(ee.ria.xroad.common.util.CachingStream) Test(org.junit.Test)

Example 9 with ProxyMessageDecoder

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());
}
Also used : ProxyMessage(ee.ria.xroad.proxy.protocol.ProxyMessage) CodedException(ee.ria.xroad.common.CodedException) ProxyMessageDecoder(ee.ria.xroad.proxy.protocol.ProxyMessageDecoder)

Example 10 with ProxyMessageDecoder

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());
}
Also used : ProxyMessage(ee.ria.xroad.proxy.protocol.ProxyMessage) CodedException(ee.ria.xroad.common.CodedException) ProxyMessageDecoder(ee.ria.xroad.proxy.protocol.ProxyMessageDecoder)

Aggregations

ProxyMessageDecoder (ee.ria.xroad.proxy.protocol.ProxyMessageDecoder)10 RestRequest (ee.ria.xroad.common.message.RestRequest)7 ServiceId (ee.ria.xroad.common.identifier.ServiceId)6 OpMonitoringData (ee.ria.xroad.common.opmonitoring.OpMonitoringData)6 ProxyMessageEncoder (ee.ria.xroad.proxy.protocol.ProxyMessageEncoder)6 Test (org.junit.Test)6 CachingStream (ee.ria.xroad.common.util.CachingStream)5 CodedException (ee.ria.xroad.common.CodedException)4 ProxyMessage (ee.ria.xroad.proxy.protocol.ProxyMessage)4 RestResponse (ee.ria.xroad.common.message.RestResponse)3 RestServiceDetailsListType (ee.ria.xroad.common.metadata.RestServiceDetailsListType)2 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 ErrorCodes.translateException (ee.ria.xroad.common.ErrorCodes.translateException)1 SoapMessageImpl (ee.ria.xroad.common.message.SoapMessageImpl)1 URISyntaxException (java.net.URISyntaxException)1