Search in sources :

Example 1 with HttpRequest

use of org.mockserver.model.HttpRequest in project hadoop by apache.

the class TestWebHDFSOAuth2 method getOAuthServerMockRequest.

private HttpRequest getOAuthServerMockRequest(MockServerClient mockServerClient) throws IOException {
    HttpRequest expectedRequest = request().withMethod("POST").withPath("/refresh").withBody("client_secret=credential&grant_type=client_credentials&client_id=MY_CLIENTID");
    Map<String, Object> map = new TreeMap<>();
    map.put(EXPIRES_IN, "0987654321");
    map.put(TOKEN_TYPE, "bearer");
    map.put(ACCESS_TOKEN, AUTH_TOKEN);
    ObjectMapper mapper = new ObjectMapper();
    HttpResponse resp = response().withStatusCode(HttpStatus.SC_OK).withHeaders(CONTENT_TYPE_APPLICATION_JSON).withBody(mapper.writeValueAsString(map));
    mockServerClient.when(expectedRequest, exactly(1)).respond(resp);
    return expectedRequest;
}
Also used : HttpRequest(org.mockserver.model.HttpRequest) HttpResponse(org.mockserver.model.HttpResponse) TreeMap(java.util.TreeMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with HttpRequest

use of org.mockserver.model.HttpRequest in project hadoop by apache.

the class TestRefreshTokenTimeBasedTokenRefresher method refreshUrlIsCorrect.

@Test
public void refreshUrlIsCorrect() throws IOException {
    final int PORT = 7552;
    final String REFRESH_ADDRESS = "http://localhost:" + PORT + "/refresh";
    long tokenExpires = 0;
    Configuration conf = buildConf("refresh token key", Long.toString(tokenExpires), "joebob", REFRESH_ADDRESS);
    Timer mockTimer = mock(Timer.class);
    when(mockTimer.now()).thenReturn(tokenExpires + 1000l);
    AccessTokenProvider tokenProvider = new ConfRefreshTokenBasedAccessTokenProvider(mockTimer);
    tokenProvider.setConf(conf);
    // Build mock server to receive refresh request
    ClientAndServer mockServer = startClientAndServer(PORT);
    HttpRequest expectedRequest = request().withMethod("POST").withPath("/refresh").withBody(ParameterBody.params(Parameter.param(CLIENT_ID, "joebob"), Parameter.param(GRANT_TYPE, REFRESH_TOKEN), Parameter.param(REFRESH_TOKEN, "refresh token key")));
    MockServerClient mockServerClient = new MockServerClient("localhost", PORT);
    // https://tools.ietf.org/html/rfc6749#section-5.1
    Map<String, Object> map = new TreeMap<>();
    map.put(EXPIRES_IN, "0987654321");
    map.put(TOKEN_TYPE, BEARER);
    map.put(ACCESS_TOKEN, "new access token");
    ObjectMapper mapper = new ObjectMapper();
    HttpResponse resp = response().withStatusCode(HttpStatus.SC_OK).withHeaders(CONTENT_TYPE_APPLICATION_JSON).withBody(mapper.writeValueAsString(map));
    mockServerClient.when(expectedRequest, exactly(1)).respond(resp);
    assertEquals("new access token", tokenProvider.getAccessToken());
    mockServerClient.verify(expectedRequest);
    mockServerClient.clear(expectedRequest);
    mockServer.stop();
}
Also used : HttpRequest(org.mockserver.model.HttpRequest) Configuration(org.apache.hadoop.conf.Configuration) HttpResponse(org.mockserver.model.HttpResponse) MockServerClient(org.mockserver.client.server.MockServerClient) TreeMap(java.util.TreeMap) Timer(org.apache.hadoop.util.Timer) ClientAndServer(org.mockserver.integration.ClientAndServer) ClientAndServer.startClientAndServer(org.mockserver.integration.ClientAndServer.startClientAndServer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with HttpRequest

use of org.mockserver.model.HttpRequest in project jaeger-client-java by jaegertracing.

the class JaegerRequestAndResponseInterceptorIntegrationTest method verifyTracing.

private void verifyTracing(Span parentSpan) {
    // Assert that traces are correctly emitted by the client
    List<Span> spans = reporter.getSpans();
    assertEquals(2, spans.size());
    Span span = spans.get(1);
    assertEquals("GET", span.getOperationName());
    assertEquals(parentSpan.context().getSpanId(), span.context().getParentId());
    // Assert traces and baggage are propagated correctly to server
    HttpRequest[] httpRequests = mockServerClient.retrieveRecordedRequests(null);
    assertEquals(1, httpRequests.length);
    String traceData = httpRequests[0].getFirstHeader("uber-trace-id");
    String[] split = traceData.split("%3A");
    assertEquals(Long.toHexString(span.context().getTraceId()), split[0]);
    assertEquals(Long.toHexString(span.context().getSpanId()), split[1]);
    String baggage = httpRequests[0].getFirstHeader("uberctx-" + BAGGAGE_KEY);
    assertEquals(BAGGAGE_VALUE, baggage);
}
Also used : HttpRequest(org.mockserver.model.HttpRequest) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) Span(com.uber.jaeger.Span)

Example 4 with HttpRequest

use of org.mockserver.model.HttpRequest in project hadoop by apache.

the class TestWebHDFSOAuth2 method listStatusReturnsAsExpected.

@Test
public void listStatusReturnsAsExpected() throws URISyntaxException, IOException {
    MockServerClient mockWebHDFSServerClient = new MockServerClient("localhost", WEBHDFS_PORT);
    MockServerClient mockOAuthServerClient = new MockServerClient("localhost", OAUTH_PORT);
    HttpRequest oauthServerRequest = getOAuthServerMockRequest(mockOAuthServerClient);
    HttpRequest fileSystemRequest = request().withMethod("GET").withPath(WebHdfsFileSystem.PATH_PREFIX + "/test1/test2").withHeader(AUTH_TOKEN_HEADER);
    try {
        mockWebHDFSServerClient.when(fileSystemRequest, exactly(1)).respond(response().withStatusCode(HttpStatus.SC_OK).withHeaders(CONTENT_TYPE_APPLICATION_JSON).withBody("{\n" + "  \"FileStatuses\":\n" + "  {\n" + "    \"FileStatus\":\n" + "    [\n" + "      {\n" + "        \"accessTime\"      : 1320171722771,\n" + "        \"blockSize\"       : 33554432,\n" + "        \"group\"           : \"supergroup\",\n" + "        \"length\"          : 24930,\n" + "        \"modificationTime\": 1320171722771,\n" + "        \"owner\"           : \"webuser\",\n" + "        \"pathSuffix\"      : \"a.patch\",\n" + "        \"permission\"      : \"644\",\n" + "        \"replication\"     : 1,\n" + "        \"type\"            : \"FILE\"\n" + "      },\n" + "      {\n" + "        \"accessTime\"      : 0,\n" + "        \"blockSize\"       : 0,\n" + "        \"group\"           : \"supergroup\",\n" + "        \"length\"          : 0,\n" + "        \"modificationTime\": 1320895981256,\n" + "        \"owner\"           : \"szetszwo\",\n" + "        \"pathSuffix\"      : \"bar\",\n" + "        \"permission\"      : \"711\",\n" + "        \"replication\"     : 0,\n" + "        \"type\"            : \"DIRECTORY\"\n" + "      }\n" + "    ]\n" + "  }\n" + "}\n"));
        FileSystem fs = new WebHdfsFileSystem();
        Configuration conf = getConfiguration();
        conf.set(OAUTH_REFRESH_URL_KEY, "http://localhost:" + OAUTH_PORT + "/refresh");
        conf.set(CredentialBasedAccessTokenProvider.OAUTH_CREDENTIAL_KEY, "credential");
        URI uri = new URI("webhdfs://localhost:" + WEBHDFS_PORT);
        fs.initialize(uri, conf);
        FileStatus[] ls = fs.listStatus(new Path("/test1/test2"));
        mockOAuthServer.verify(oauthServerRequest);
        mockWebHDFSServerClient.verify(fileSystemRequest);
        assertEquals(2, ls.length);
        assertEquals("a.patch", ls[0].getPath().getName());
        assertEquals("bar", ls[1].getPath().getName());
        fs.close();
    } finally {
        mockWebHDFSServerClient.clear(fileSystemRequest);
        mockOAuthServerClient.clear(oauthServerRequest);
    }
}
Also used : HttpRequest(org.mockserver.model.HttpRequest) Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) Configuration(org.apache.hadoop.conf.Configuration) FileSystem(org.apache.hadoop.fs.FileSystem) MockServerClient(org.mockserver.client.server.MockServerClient) URI(java.net.URI) Test(org.junit.Test)

Example 5 with HttpRequest

use of org.mockserver.model.HttpRequest in project hadoop by apache.

the class TestClientCredentialTimeBasedTokenRefresher method refreshUrlIsCorrect.

@Test
public void refreshUrlIsCorrect() throws IOException {
    final int PORT = ServerSocketUtil.getPort(0, 20);
    final String REFRESH_ADDRESS = "http://localhost:" + PORT + "/refresh";
    long tokenExpires = 0;
    Configuration conf = buildConf("myreallycoolcredential", Long.toString(tokenExpires), CLIENT_ID_FOR_TESTING, REFRESH_ADDRESS);
    Timer mockTimer = mock(Timer.class);
    when(mockTimer.now()).thenReturn(tokenExpires + 1000l);
    AccessTokenProvider credProvider = new ConfCredentialBasedAccessTokenProvider(mockTimer);
    credProvider.setConf(conf);
    // Build mock server to receive refresh request
    ClientAndServer mockServer = startClientAndServer(PORT);
    HttpRequest expectedRequest = request().withMethod("POST").withPath("/refresh").withBody(// it ourselves via the ordering provided to ParameterBody...
    ParameterBody.params(Parameter.param(CLIENT_SECRET, "myreallycoolcredential"), Parameter.param(GRANT_TYPE, CLIENT_CREDENTIALS), Parameter.param(CLIENT_ID, CLIENT_ID_FOR_TESTING)));
    MockServerClient mockServerClient = new MockServerClient("localhost", PORT);
    // https://tools.ietf.org/html/rfc6749#section-5.1
    Map<String, Object> map = new TreeMap<>();
    map.put(EXPIRES_IN, "0987654321");
    map.put(TOKEN_TYPE, "bearer");
    map.put(ACCESS_TOKEN, "new access token");
    ObjectMapper mapper = new ObjectMapper();
    HttpResponse resp = response().withStatusCode(HttpStatus.SC_OK).withHeaders(CONTENT_TYPE_APPLICATION_JSON).withBody(mapper.writeValueAsString(map));
    mockServerClient.when(expectedRequest, exactly(1)).respond(resp);
    assertEquals("new access token", credProvider.getAccessToken());
    mockServerClient.verify(expectedRequest);
    mockServerClient.clear(expectedRequest);
    mockServer.stop();
}
Also used : HttpRequest(org.mockserver.model.HttpRequest) Configuration(org.apache.hadoop.conf.Configuration) HttpResponse(org.mockserver.model.HttpResponse) MockServerClient(org.mockserver.client.server.MockServerClient) TreeMap(java.util.TreeMap) Timer(org.apache.hadoop.util.Timer) ClientAndServer(org.mockserver.integration.ClientAndServer) ClientAndServer.startClientAndServer(org.mockserver.integration.ClientAndServer.startClientAndServer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

HttpRequest (org.mockserver.model.HttpRequest)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Test (org.junit.Test)4 TreeMap (java.util.TreeMap)3 Configuration (org.apache.hadoop.conf.Configuration)3 MockServerClient (org.mockserver.client.server.MockServerClient)3 HttpResponse (org.mockserver.model.HttpResponse)3 Timer (org.apache.hadoop.util.Timer)2 ClientAndServer (org.mockserver.integration.ClientAndServer)2 ClientAndServer.startClientAndServer (org.mockserver.integration.ClientAndServer.startClientAndServer)2 Span (com.uber.jaeger.Span)1 URI (java.net.URI)1 FileStatus (org.apache.hadoop.fs.FileStatus)1 FileSystem (org.apache.hadoop.fs.FileSystem)1 Path (org.apache.hadoop.fs.Path)1 BasicHttpRequest (org.apache.http.message.BasicHttpRequest)1 AddFilesRequest (org.nzbhydra.downloading.AddFilesRequest)1 SearchResultWebTO (org.nzbhydra.searching.SearchResultWebTO)1 DataJpaTest (org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1