org.mockserver.model.HttpResponse Java Examples

The following examples show how to use org.mockserver.model.HttpResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpClientTest.java    From pmq with Apache License 2.0 7 votes vote down vote up
@Test
public void getErrorTest() throws IOException {
	ClientAndServer mockClient = new ClientAndServer(5000);
	try {
		mockClient.when(HttpRequest.request().withMethod("GET"))
				.respond(HttpResponse.response().withStatusCode(700).withBody("1"));
		HttpClient httpClient = new HttpClient();
		boolean rs = false;
		try {
			assertEquals("1", httpClient.get("http://localhost:5000/hs"));
		} catch (Exception e) {
			rs = true;
		}
		assertEquals(true, rs);
	} finally {
		mockClient.stop(true);
	}
}
 
Example #2
Source File: TestTusClient.java    From tus-java-client with MIT License 6 votes vote down vote up
@Test
public void testResumeUpload() throws ResumingNotEnabledException, FingerprintNotFoundException, IOException, ProtocolException {
    mockServer.when(new HttpRequest()
            .withMethod("HEAD")
            .withPath("/files/foo")
            .withHeader("Tus-Resumable", TusClient.TUS_VERSION))
            .respond(new HttpResponse()
                    .withStatusCode(204)
                    .withHeader("Tus-Resumable", TusClient.TUS_VERSION)
                    .withHeader("Upload-Offset", "3"));

    TusClient client = new TusClient();
    client.setUploadCreationURL(mockServerURL);
    client.enableResuming(new TestResumeUploadStore());

    TusUpload upload = new TusUpload();
    upload.setSize(10);
    upload.setInputStream(new ByteArrayInputStream(new byte[10]));
    upload.setFingerprint("test-fingerprint");

    TusUploader uploader = client.resumeUpload(upload);

    assertEquals(uploader.getUploadURL(), new URL(mockServerURL.toString() + "/foo"));
    assertEquals(uploader.getOffset(), 3);
}
 
Example #3
Source File: RsExplorerTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void findInvalidSitemapDocument() throws Exception {
  String path = "/.well-known/resourcesync";

  getMockServer()
    .when(HttpRequest.request()
        .withMethod("GET")
        .withPath(path),
      Times.exactly(1))

    .respond(HttpResponse.response()
      .withStatusCode(200)
      .withBody(createValidXml())
    );

  RsExplorer explorer = new RsExplorer(getHttpclient(), getRsContext());
  ResultIndex index = new ResultIndex();
  Result<RsRoot> result = explorer.explore(composeUri(path), index, null);

  assertThat(result.getUri(), equalTo(composeUri(path)));
  assertThat(result.getStatusCode(), equalTo(200));
  assertThat(result.getErrors().isEmpty(), is(false));
  assertThat(result.getErrors().get(0), instanceOf(JAXBException.class));
  assertThat(result.getContent().isPresent(), is(false));
  //result.listErrors().forEach(Throwable::printStackTrace);
}
 
Example #4
Source File: RsExplorerTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void receiveStatusError() throws Exception {
  String path = "/.well-known/resourcesync";

  getMockServer()
    .when(HttpRequest.request()
        .withMethod("GET")
        .withPath(path),
      Times.exactly(1))

    .respond(HttpResponse.response()
      .withStatusCode(404)
      .withBody("Document not found")
    );

  RsExplorer explorer = new RsExplorer(getHttpclient(), getRsContext());
  ResultIndex index = new ResultIndex();
  Result<RsRoot> result = explorer.explore(composeUri(path), index, null);

  assertThat(result.getUri(), equalTo(composeUri(path)));
  assertThat(result.getStatusCode(), equalTo(404));
  assertThat(result.getErrors().isEmpty(), is(false));
  assertThat(result.getErrors().get(0), instanceOf(RemoteException.class));
  assertThat(result.getContent().isPresent(), is(false));
  //result.getDataSetErrors().forEach(Throwable::printStackTrace);
}
 
Example #5
Source File: HttpClientTest.java    From pmq with Apache License 2.0 6 votes vote down vote up
@Test
public void getError1Test() throws IOException {
	ClientAndServer mockClient = new ClientAndServer(5000);
	try {
		mockClient.when(HttpRequest.request().withMethod("GET"))
				.respond(HttpResponse.response().withStatusCode(700).withBody("1"));
		HttpClient httpClient = new HttpClient();
		boolean rs = false;
		try {
			assertEquals("1", httpClient.get("http://localhost:50001/hs"));
		} catch (Exception e) {
			rs = true;
		}
		assertEquals(true, rs);
	} finally {
		mockClient.stop(true);
	}
}
 
Example #6
Source File: MockServerTestHelper.java    From mutual-tls-ssl with Apache License 2.0 6 votes vote down vote up
public MockServerTestHelper(ClientType clientType) {
    clientAndServer = ClientAndServer.startClientAndServer(8080);
    mockServerClient = new MockServerClient("127.0.0.1", 8080);
    mockServerClient
            .when(
                    HttpRequest.request()
                            .withMethod("GET")
                            .withPath("/api/hello")
                            .withHeader("client-type", clientType.getValue()),
                    Times.unlimited(),
                    TimeToLive.unlimited())
            .respond(
                    HttpResponse.response()
                            .withBody("Hello")
                            .withStatusCode(200)
            );
}
 
Example #7
Source File: HttpClientTest.java    From pmq with Apache License 2.0 6 votes vote down vote up
@Test
public void postError3Test() throws IOException {
	ClientAndServer mockClient = new ClientAndServer(5000);
	try {
		mockClient.when(HttpRequest.request().withMethod("POST"))
				.respond(HttpResponse.response().withStatusCode(200).withBody("1"));
		HttpClient httpClient = new HttpClient();
		boolean rs = false;
		try {
			assertEquals(1, httpClient.post("http://localhost:5001/hs", "1", TestDemo.class).getT());
		} catch (Exception e) {
			rs = true;
		}
		assertEquals(true, rs);
	} finally {
		mockClient.stop(true);
	}
}
 
Example #8
Source File: HttpClientTest.java    From pmq with Apache License 2.0 6 votes vote down vote up
@Test
public void postError2Test() throws IOException {
	ClientAndServer mockClient = new ClientAndServer(5000);
	try {
		mockClient.when(HttpRequest.request().withMethod("POST"))
				.respond(HttpResponse.response().withStatusCode(700).withBody("1"));
		HttpClient httpClient = new HttpClient();
		boolean rs = false;
		try {
			assertEquals(1, httpClient.post("http://localhost:5000/hs", "1", TestDemo.class).getT());
		} catch (Exception e) {
			rs = true;
		}
		assertEquals(true, rs);
	} finally {
		mockClient.stop(true);
	}
}
 
Example #9
Source File: HttpClientTest.java    From pmq with Apache License 2.0 6 votes vote down vote up
@Test
public void postError1Test() throws IOException {
	ClientAndServer mockClient = new ClientAndServer(5000);
	try {
		mockClient.when(HttpRequest.request().withMethod("POST"))
				.respond(HttpResponse.response().withStatusCode(200).withBody("1"));
		HttpClient httpClient = new HttpClient();
		boolean rs = false;
		try {
			assertEquals("1", httpClient.post("http://localhost:5001/hs", "1"));
		} catch (Exception e) {
			rs = true;
		}
		assertEquals(true, rs);
	} finally {
		mockClient.stop(true);
	}
}
 
Example #10
Source File: HttpClientTest.java    From pmq with Apache License 2.0 6 votes vote down vote up
@Test
public void postErrorTest() throws IOException {
	ClientAndServer mockClient = new ClientAndServer(5000); 
	try {
		mockClient.when(HttpRequest.request().withMethod("POST"))
				.respond(HttpResponse.response().withStatusCode(700).withBody("1"));
		HttpClient httpClient = new HttpClient();
		boolean rs = false;
		try {
			assertEquals("1", httpClient.post("http://localhost:5000/hs", "1"));
		} catch (Exception e) {
			rs = true;
		}
		assertEquals(true, rs);
	} finally {
		mockClient.stop(true);
	}
}
 
Example #11
Source File: AcmeMockServerBuilder.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public AcmeMockServerBuilder addCheckOrderRequestAndResponse(String orderUrl, String checkCertificateResponseBody, int checkCertificateStatusCode, String checkOrderReplayNonce) {
    HttpResponse response = response()
            .withHeader("Retry-After", "0")
            .withHeader("Cache-Control", "public, max-age=0, no-cache")
            .withHeader("Content-Type", "application/json")
            .withHeader("Replay-Nonce", checkOrderReplayNonce)
            .withBody(checkCertificateResponseBody)
            .withStatusCode(checkCertificateStatusCode);
    server.when(
            request()
                    .withMethod("POST")
                    .withPath(orderUrl)
                    .withBody(""),
            Times.once())
            .respond(response);

    return this;
}
 
Example #12
Source File: AcmeMockServerBuilder.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public AcmeMockServerBuilder addCertificateRequestAndResponse(String certificateUrl, String certificateResponseBody, int certificateStatusCode, String certificateReplayNonce) {
    HttpResponse response = response()
            .withHeader("Retry-After", "0")
            .withHeader("Cache-Control", "public, max-age=0, no-cache")
            .withHeader("Content-Type", "application/pem-certificate-chain")
            .withHeader("Replay-Nonce", certificateReplayNonce)
            .withBody(certificateResponseBody)
            .withStatusCode(certificateStatusCode);
    server.when(
            request()
                    .withMethod("POST")
                    .withPath(certificateUrl)
                    .withBody(""),
            Times.once())
            .respond(response);

    return this;
}
 
Example #13
Source File: HealthCheckServiceUnitTest.java    From galeb with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setupClass() {
    environmentVariables.set("ENVIRONMENT_NAME", "env1");
    environmentVariables.set("ZONE_ID", "zone1");
    
    mockServer = ClientAndServer.startClientAndServer(BACKEND_PORT);

    mockServer.when(HttpRequest.request()
                    .withMethod("GET")
                    .withPath("/")
                    .withHeaders(
                            Header.header("user-agent", "Galeb_HealthChecker/1.0"),
                            Header.header(NottableString.not("cookie")))
                    .withKeepAlive(true))
              .respond(HttpResponse.response()
                    .withCookie(new Cookie("session", "test-cookie"))
                    .withStatusCode(HttpStatus.OK.value()));
    
    mockTCPOnlyServer();
}
 
Example #14
Source File: RegistryClientTest.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Test
public void missingImageTest() {
    final String repo = "titusops/alpine";
    final String tag = "doesnotexist";

    mockServer
            .when(
                    HttpRequest.request().withPath("/v2/" + repo + "/manifests/" + tag)
            ).respond(HttpResponse.response()
            .withStatusCode(HttpResponseStatus.NOT_FOUND.code()));

    try {
        registryClient.getImageDigest(repo, tag).timeout(TIMEOUT).block();
    } catch (TitusRegistryException e) {
        assertThat(e.getErrorCode()).isEqualTo(TitusRegistryException.ErrorCode.IMAGE_NOT_FOUND);
    }
}
 
Example #15
Source File: DatadogSecretsDoNotLeakWhenApiCalledFunctionalTest.java    From kayenta with Apache License 2.0 6 votes vote down vote up
@Test
public void
    test_that_the_datadog_remote_service_does_not_log_the_api_key_when_getMetrics_is_called() {
  DatadogMetricDescriptorsResponse mockResponse = new DatadogMetricDescriptorsResponse();
  String mockResponseAsString;
  try {
    mockResponseAsString = objectMapper.writeValueAsString(mockResponse);
  } catch (JsonProcessingException e) {
    throw new RuntimeException("Failed to serialize mock DatadogTimeSeries response");
  }

  mockServerClient
      .when(HttpRequest.request())
      .respond(HttpResponse.response(mockResponseAsString));

  DatadogMetricDescriptorsResponse res =
      datadogRemoteService.getMetrics(
          API_KEY, APPLICATION_KEY, Instant.now().minus(5, ChronoUnit.MINUTES).toEpochMilli());

  assertEquals(mockResponse, res);
  assertTrue("We expected there to be at least 1 logged message", listAppender.list.size() > 0);
  assertMessagesDoNotContainSecrets(listAppender.list);
}
 
Example #16
Source File: TestTusClient.java    From tus-java-client with MIT License 6 votes vote down vote up
@Test
public void testResumeOrCreateUpload() throws IOException, ProtocolException {
    mockServer.when(new HttpRequest()
            .withMethod("POST")
            .withPath("/files")
            .withHeader("Tus-Resumable", TusClient.TUS_VERSION)
            .withHeader("Upload-Length", "10"))
            .respond(new HttpResponse()
                    .withStatusCode(201)
                    .withHeader("Tus-Resumable", TusClient.TUS_VERSION)
                    .withHeader("Location", mockServerURL + "/foo"));

    TusClient client = new TusClient();
    client.setUploadCreationURL(mockServerURL);
    TusUpload upload = new TusUpload();
    upload.setSize(10);
    upload.setInputStream(new ByteArrayInputStream(new byte[10]));
    TusUploader uploader = client.resumeOrCreateUpload(upload);

    assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo"));
}
 
Example #17
Source File: TestTusClient.java    From tus-java-client with MIT License 6 votes vote down vote up
@Test
public void testCreateUploadWithMissingLocationHeader() throws IOException, Exception {
    mockServer.when(new HttpRequest()
            .withMethod("POST")
            .withPath("/files")
            .withHeader("Tus-Resumable", TusClient.TUS_VERSION)
            .withHeader("Upload-Length", "10"))
            .respond(new HttpResponse()
                    .withStatusCode(201)
                    .withHeader("Tus-Resumable", TusClient.TUS_VERSION));

    TusClient client = new TusClient();
    client.setUploadCreationURL(mockServerURL);
    TusUpload upload = new TusUpload();
    upload.setSize(10);
    upload.setInputStream(new ByteArrayInputStream(new byte[10]));
    try {
        TusUploader uploader = client.createUpload(upload);
        throw new Exception("unreachable code reached");
    } catch(ProtocolException e) {
        assertEquals(e.getMessage(), "missing upload URL in response for creating upload");
    }
}
 
Example #18
Source File: TestTusClient.java    From tus-java-client with MIT License 6 votes vote down vote up
@Test
public void testBeginOrResumeUploadFromURL() throws IOException, ProtocolException {
    mockServer.when(new HttpRequest()
            .withMethod("HEAD")
            .withPath("/files/fooFromURL")
            .withHeader("Tus-Resumable", TusClient.TUS_VERSION))
            .respond(new HttpResponse()
                    .withStatusCode(204)
                    .withHeader("Tus-Resumable", TusClient.TUS_VERSION)
                    .withHeader("Upload-Offset", "3"));

    TusClient client = new TusClient();
    URL uploadURL = new URL(mockServerURL.toString() + "/fooFromURL");

    TusUpload upload = new TusUpload();
    upload.setSize(10);
    upload.setInputStream(new ByteArrayInputStream(new byte[10]));

    TusUploader uploader = client.beginOrResumeUploadFromURL(upload, uploadURL);

    assertEquals(uploader.getUploadURL(), uploadURL);
    assertEquals(uploader.getOffset(), 3);
}
 
Example #19
Source File: TestAuth.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnableAuth() throws Exception {
  VaultConfiguration conf = VaultConfigurationBuilder.newVaultConfiguration()
      .withAddress(address)
      .withToken(token)
      .build();

  new MockServerClient(localhost, mockServerRule.getPort())
      .when(
      HttpRequest.request()
          .withMethod("POST")
          .withHeader(tokenHeader)
          .withPath("/v1/sys/auth/app-id")
          .withBody("{\"type\":\"app-id\"}"),
      Times.exactly(1)
  )
  .respond(
      HttpResponse.response()
          .withStatusCode(HttpStatusCode.NO_CONTENT_204.code())
          .withHeader(contentJson)
  );

  VaultClient vault = new VaultClient(conf);
  assertTrue(vault.sys().auth().enable("app-id", "app-id"));
}
 
Example #20
Source File: TunnelTest.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
@Test(enabled = false)
public void mustDownloadLargeFiles()
    throws Exception {

  mockServer.when(HttpRequest.request().withMethod("CONNECT").withPath("www.us.apache.org:80"))
      .respond(HttpResponse.response().withStatusCode(200));
  mockServer.when(HttpRequest.request().withMethod("GET")
      .withPath("/dist//httpcomponents/httpclient/binary/httpcomponents-client-4.5.1-bin.tar.gz"))
      .forward(HttpForward.forward().withHost("www.us.apache.org").withPort(80));

  Tunnel tunnel = Tunnel.build("www.us.apache.org", 80, "localhost", PORT);
  try {
    IOUtils.copyLarge((InputStream) new URL("http://localhost:" + tunnel.getPort()
            + "/dist//httpcomponents/httpclient/binary/httpcomponents-client-4.5.1-bin.tar.gz")
            .getContent(new Class[]{InputStream.class}),
        new FileOutputStream(File.createTempFile("httpcomponents-client-4.5.1-bin", "tar.gz")));
  } finally {
    tunnel.close();
  }
}
 
Example #21
Source File: LowLatencyMontageClientTest.java    From render with GNU General Public License v2.0 6 votes vote down vote up
private void addRenderStackMetaDataResponse() {
    final String requestPath = getRenderStackRequestPath();
    final StackMetaData stackMetaData = new StackMetaData(acquireStackId, null);
    stackMetaData.setState(StackMetaData.StackState.LOADING);
    final JsonBody responseBody = json(stackMetaData.toJson());
    mockServer
            .when(
                    HttpRequest.request()
                            .withMethod("GET")
                            .withPath(requestPath),
                    Times.once()
            )
            .respond(
                    HttpResponse.response()
                            .withStatusCode(HttpStatus.SC_OK)
                            .withHeader("Content-Type", responseBody.getContentType())
                            .withBody(responseBody)
            );
}
 
Example #22
Source File: LowLatencyMontageClientTest.java    From render with GNU General Public License v2.0 6 votes vote down vote up
private void addAcqNextTileResponse(final AcquisitionTileList acquisitionTileList) {

        final JsonBody responseBody = json(acquisitionTileList.toJson());

        mockServer
                .when(
                        HttpRequest.request()
                                .withMethod("POST")
                                .withPath(getBaseAcquisitionPath() + "/next-tile"),
                        Times.once()
                )
                .respond(
                        HttpResponse.response()
                                .withStatusCode(HttpStatus.SC_OK)
                                .withHeader("Content-Type", responseBody.getContentType())
                                .withBody(responseBody)
        );
    }
 
Example #23
Source File: AcmeMockServerBuilder.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public AcmeMockServerBuilder addCertificateRequestAndResponse(String certificateUrl, String expectedCertificateRequestBody, String certificateResponseBody, String certificateReplayNonce, int certificateStatusCode) {
    HttpResponse response = response()
            .withHeader("Cache-Control", "public, max-age=0, no-cache")
            .withHeader("Content-Type", "application/pem-certificate-chain")
            .withHeader("Replay-Nonce", certificateReplayNonce)
            .withBody(certificateResponseBody)
            .withStatusCode(certificateStatusCode);
    server.when(
            request()
                    .withMethod("POST")
                    .withPath(certificateUrl)
                    .withBody(expectedCertificateRequestBody),
            Times.once())
            .respond(response);

    return this;
}
 
Example #24
Source File: AcmeMockServerBuilder.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public AcmeMockServerBuilder addCheckOrderRequestAndResponse(String orderUrl, String expectedCheckCertificateRequestBody, String checkCertificateResponseBody, String checkOrderReplayNonce, int checkCertificateStatusCode) {
    HttpResponse response = response()
            .withHeader("Cache-Control", "public, max-age=0, no-cache")
            .withHeader("Content-Type", "application/json")
            .withHeader("Replay-Nonce", checkOrderReplayNonce)
            .withBody(checkCertificateResponseBody)
            .withStatusCode(checkCertificateStatusCode);
    server.when(
            request()
                    .withMethod("POST")
                    .withPath(orderUrl)
                    .withBody(expectedCheckCertificateRequestBody),
            Times.once())
            .respond(response);

    return this;
}
 
Example #25
Source File: RegistryClientTest.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Test
public void getDigestTest() {
    final String repo = "titusops/alpine";
    final String tag = "latest";
    final String digest = "sha256:f9f5bb506406b80454a4255b33ed2e4383b9e4a32fb94d6f7e51922704e818fa";

    mockServer
            .when(
                    HttpRequest.request()
                            .withMethod("GET")
                            .withPath("/v2/" + repo + "/manifests/" + tag)
            )
            .respond(
                    HttpResponse.response()
                            .withStatusCode(HttpResponseStatus.OK.code())
                            .withHeader(
                                    new Header("Docker-Content-Digest", digest)
                            )
                            .withBody("{\"schemaVersion\": 2}")
            );

    String retrievedDigest = registryClient.getImageDigest(repo, tag).timeout(TIMEOUT).block();
    assertThat(retrievedDigest).isEqualTo(digest);
}
 
Example #26
Source File: LowLatencyMontageClientTest.java    From render with GNU General Public License v2.0 5 votes vote down vote up
private void addRenderResolvedTilesResponse() {
    final String requestPath = getRenderStackRequestPath() + "/resolvedTiles";
    mockServer
            .when(
                    HttpRequest.request()
                            .withMethod("PUT")
                            .withPath(requestPath),
                    Times.once()
            )
            .respond(
                    HttpResponse.response()
                            .withStatusCode(HttpStatus.SC_CREATED)
    );

}
 
Example #27
Source File: TestLease.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testRenew() throws Exception {
  VaultConfiguration conf = VaultConfigurationBuilder.newVaultConfiguration()
      .withAddress(address)
      .withToken(token)
      .build();

  String leaseId = "aws/creds/s3ReadOnly/895aad27-028e-301b-5475-906a6561f8f8";

  new MockServerClient(localhost, mockServerRule.getPort())
      .when(
      HttpRequest.request()
          .withMethod("PUT")
          .withHeader(tokenHeader)
          .withPath("/v1/sys/renew/" + leaseId),
      Times.exactly(1)
  )
  .respond(
      HttpResponse.response()
          .withStatusCode(HttpStatusCode.OK_200.code())
          .withHeader(contentJson)
          .withBody(getBody("sys_lease_renew.json"))
  );

  VaultClient vault = new VaultClient(conf);
  Secret secret = vault.sys().lease().renew(leaseId);
  Assert.assertEquals(leaseId, secret.getLeaseId());
  Assert.assertEquals(60, secret.getLeaseDuration());
  Assert.assertEquals(true, secret.isRenewable());
}
 
Example #28
Source File: TunnelTest.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@Test(enabled=false, expectedExceptions = SocketException.class)
public void mustRefuseConnectionWhenProxyTimesOut() throws Exception{
  mockServer.when(HttpRequest.request().withMethod("CONNECT").withPath("www.us.apache.org:80"))
      .respond(HttpResponse.response().withDelay(TimeUnit.SECONDS,2).withStatusCode(200));

  Tunnel tunnel = Tunnel.build("example.org", 80, "localhost", PORT);

  try {
    int tunnelPort = tunnel.getPort();

    fetchContent(tunnelPort);
  } finally {
    tunnel.close();
  }
}
 
Example #29
Source File: TestTusClient.java    From tus-java-client with MIT License 5 votes vote down vote up
@Test
public void testResumeOrCreateUploadNotFound() throws IOException, ProtocolException {
    mockServer.when(new HttpRequest()
            .withMethod("HEAD")
            .withPath("/files/not_found")
            .withHeader("Tus-Resumable", TusClient.TUS_VERSION))
            .respond(new HttpResponse()
                    .withStatusCode(404));

    mockServer.when(new HttpRequest()
            .withMethod("POST")
            .withPath("/files")
            .withHeader("Tus-Resumable", TusClient.TUS_VERSION)
            .withHeader("Upload-Length", "10"))
            .respond(new HttpResponse()
                    .withStatusCode(201)
                    .withHeader("Tus-Resumable", TusClient.TUS_VERSION)
                    .withHeader("Location", mockServerURL + "/foo"));

    TusClient client = new TusClient();
    client.setUploadCreationURL(mockServerURL);

    TusURLStore store = new TusURLMemoryStore();
    store.set("fingerprint", new URL(mockServerURL + "/not_found"));
    client.enableResuming(store);

    TusUpload upload = new TusUpload();
    upload.setSize(10);
    upload.setInputStream(new ByteArrayInputStream(new byte[10]));
    upload.setFingerprint("fingerprint");
    TusUploader uploader = client.resumeOrCreateUpload(upload);

    assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo"));
}
 
Example #30
Source File: TunnelTest.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@Test(enabled=false, expectedExceptions = SocketException.class)
public void mustRefuseConnectionWhenProxyRefuses() throws Exception{
  mockServer.when(HttpRequest.request().withMethod("CONNECT").withPath("www.us.apache.org:80"))
      .respond(HttpResponse.response().withStatusCode(403));

  Tunnel tunnel = Tunnel.build("example.org", 80, "localhost", PORT);

  try {
    int tunnelPort = tunnel.getPort();

    fetchContent(tunnelPort);
  } finally {
    tunnel.close();
  }
}