com.github.tomakehurst.wiremock.http.RequestMethod Java Examples

The following examples show how to use com.github.tomakehurst.wiremock.http.RequestMethod. 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: HttpMethodEndpointTest.java    From RoboZombie with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Test for the request method PUT.</p>
 * 
 * @since 1.3.0
 */
@Test
public final void testPutMethod() {
	
	Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
	
	String path = "/putrequest";
	
	stubFor(put(urlEqualTo(path))
			.willReturn(aResponse()
			.withStatus(200)));
	
	String user = "{ '_id':1, 'alias':'Black Bolt' }";
	
	httpMethodEndpoint.putRequest(user);
	
	List<LoggedRequest> requests = findAll(putRequestedFor(urlMatching(path)));
	assertFalse(requests == null);
	assertFalse(requests.isEmpty());
	
	LoggedRequest request = requests.get(0);
	assertTrue(request.getMethod().equals(RequestMethod.PUT));
	
	String body = request.getBodyAsString();
	assertTrue(body.contains(user));
}
 
Example #2
Source File: SdkHttpClientTestSuite.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
private void validateResponse(HttpExecuteResponse response, int returnCode, SdkHttpMethod method) throws IOException {
    RequestMethod requestMethod = RequestMethod.fromString(method.name());

    RequestPatternBuilder patternBuilder = RequestPatternBuilder.newRequestPattern(requestMethod, urlMatching("/"))
                                                                       .withHeader("Host", containing("localhost"))
                                                                       .withHeader("User-Agent", equalTo("hello-world!"));

    if (method == SdkHttpMethod.HEAD) {
        patternBuilder.withRequestBody(equalTo(""));
    } else {
        patternBuilder.withRequestBody(equalTo("Body"));
    }

    mockServer.verify(1, patternBuilder);

    if (method == SdkHttpMethod.HEAD) {
        assertThat(response.responseBody()).isEmpty();
    } else {
        assertThat(IoUtils.toUtf8String(response.responseBody().orElse(null))).isEqualTo("hello");
    }

    assertThat(response.httpResponse().firstMatchingHeader("Some-Header")).contains("With Value");
    assertThat(response.httpResponse().statusCode()).isEqualTo(returnCode);
    mockServer.resetMappings();
}
 
Example #3
Source File: HttpMethodEndpointTest.java    From RoboZombie with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Test for the request method GET.</p>
 * 
 * @since 1.3.0
 */
@Test
public final void testGetMethod() {
	
	Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
	
	String name = "James-Howlett", age = "116", location = "X-Mansion";
	String path = "/getrequest?name=" + name + "&age=" + age + "&location=" + location;
	
	stubFor(get(urlEqualTo(path))
			.willReturn(aResponse()
			.withStatus(200)));
	
	httpMethodEndpoint.getRequest(name, age, location);
	
	List<LoggedRequest> requests = findAll(getRequestedFor(urlEqualTo(path)));
	assertFalse(requests == null);
	assertFalse(requests.isEmpty());
	
	LoggedRequest request = requests.get(0);
	assertTrue(request.getMethod().equals(RequestMethod.GET));
}
 
Example #4
Source File: HttpMethodEndpointTest.java    From RoboZombie with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Test for the request method POST.</p>
 * 
 * @since 1.3.0
 */
@Test
public final void testPostMethod() {
	
	Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
	
	String path = "/postrequest";
	
	stubFor(post(urlEqualTo(path))
			.willReturn(aResponse()
			.withStatus(200)));
	
	String name = "DoctorWho", age = "953", location = "Tardis";
	
	httpMethodEndpoint.postRequest(name, age, location);
	
	List<LoggedRequest> requests = findAll(postRequestedFor(urlMatching(path)));
	assertFalse(requests == null);
	assertFalse(requests.isEmpty());
	
	LoggedRequest request = requests.get(0);
	assertTrue(request.getMethod().equals(RequestMethod.POST));
	
	String body = request.getBodyAsString();
	assertTrue(body.contains("name=" + name));
	assertTrue(body.contains("age=" + age));
	assertTrue(body.contains("location=" + location));
}
 
Example #5
Source File: WireMockTestUtils.java    From junit-servers with MIT License 5 votes vote down vote up
/**
 * Verify that a given request has been triggered.
 *
 * @param endpoint Request endpoint.
 * @param method Request method.
 * @param cookies Cookies sent in HTTP request.
 */
static void assertRequestWithCookies(String endpoint, HttpMethod method, Iterable<Pair> cookies) {
	UrlPattern urlPattern = urlEqualTo(endpoint);
	RequestMethod rqMethod = new RequestMethod(method.name());
	RequestPatternBuilder rq = new RequestPatternBuilder(rqMethod, urlPattern);

	for (Pair cookie : cookies) {
		String cookieName = cookie.getO1();
		String cookieValue = cookie.getO2().get(0);
		rq.withCookie(cookieName, equalTo(cookieValue));
	}

	WireMock.verify(1, rq);
}
 
Example #6
Source File: WebhookDefinition.java    From wiremock-webhooks-extension with Apache License 2.0 5 votes vote down vote up
@JsonCreator
public WebhookDefinition(@JsonProperty("method") RequestMethod method,
                         @JsonProperty("url") URI url,
                         @JsonProperty("headers") HttpHeaders headers,
                         @JsonProperty("body") String body,
                         @JsonProperty("base64Body") String base64Body) {
    this.method = method;
    this.url = url;
    this.headers = newArrayList(headers.all());
    this.body = Body.fromOneOf(null, body, null, base64Body);
}
 
Example #7
Source File: WireMockTestUtils.java    From junit-servers with MIT License 5 votes vote down vote up
/**
 * Verify that a given request has been triggered.
 *
 * @param endpoint Request endpoint.
 * @param method Request method.
 * @param body Request body.
 */
static void assertRequestWithBody(String endpoint, HttpMethod method, String body) {
	UrlPattern urlPattern = urlEqualTo(endpoint);
	RequestMethod rqMethod = new RequestMethod(method.name());
	RequestPatternBuilder rq = new RequestPatternBuilder(rqMethod, urlPattern);
	rq.withRequestBody(equalTo(body));
	WireMock.verify(1, rq);
}
 
Example #8
Source File: WireMockTestUtils.java    From junit-servers with MIT License 5 votes vote down vote up
/**
 * Verify that a given request has been triggered.
 *
 * @param endpoint Request endpoint.
 * @param method Request method.
 */
static void assertUploadRequest(String endpoint, HttpMethod method, File file) {
	UrlPattern urlPattern = urlEqualTo(endpoint);
	RequestMethod rqMethod = new RequestMethod(method.name());
	RequestPatternBuilder rq = new RequestPatternBuilder(rqMethod, urlPattern)
		.withAllRequestBodyParts(new MultipartValuePatternBuilder()
			.withName(file.getName())
			.withBody(new BinaryEqualToPattern(TestUtils.readFile(file)))
		);

	WireMock.verify(1, rq);
}
 
Example #9
Source File: ApmServerConfigurationSourceTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadRemoteConfig() throws Exception {
    configurationSource.fetchConfig(config);
    assertThat(configurationSource.getValue("foo")).isEqualTo("bar");
    mockApmServer.verify(postRequestedFor(urlEqualTo("/config/v1/agents")));
    configurationSource.fetchConfig(config);
    mockApmServer.verify(postRequestedFor(urlEqualTo("/config/v1/agents")).withHeader("If-None-Match", equalTo("foo")));
    for (LoggedRequest request : WireMock.findAll(newRequestPattern(RequestMethod.POST, urlEqualTo("/config/v1/agents")))) {
        final JsonNode jsonNode = new ObjectMapper().readTree(request.getBodyAsString());
        assertThat(jsonNode.get("service")).isNotNull();
        assertThat(jsonNode.get("system")).isNotNull();
        assertThat(jsonNode.get("process")).isNotNull();
    }
}
 
Example #10
Source File: WireMockTestUtils.java    From junit-servers with MIT License 5 votes vote down vote up
/**
 * Verify that a given request has been triggered.
 *
 * @param endpoint Request endpoint.
 * @param method Request method.
 */
static void assertRequest(String endpoint, HttpMethod method) {
	UrlPattern urlPattern = urlEqualTo(endpoint);
	RequestMethod rqMethod = new RequestMethod(method.name());
	RequestPatternBuilder rq = new RequestPatternBuilder(rqMethod, urlPattern);
	WireMock.verify(1, rq);
}
 
Example #11
Source File: GitHubSCMProbeTest.java    From github-branch-source-plugin with MIT License 4 votes vote down vote up
@Issue("JENKINS-54126")
@Test
public void statWhenRoot404andThenIncorrectCached() throws Exception {
    GitHubSCMSource.setCacheSize(10);

    createProbeForPR(9);

    // JENKINS-54126 happens when:
    // 1. client asks for a resource "Z" that doesn't exist
    // ---> client receives a 404 response from github and caches it.
    // ---> Important: GitHub does not send ETag header for 404 responses.
    // 2. Resource "Z" gets created on GitHub but some means.
    // 3. client (eventually) asks for the resource "Z" again.
    // ---> Since the the client has a cached response without ETag, it sends "If-Modified-Since" header
    // ---> Resource has changed (it was created).
    //
    // ---> EXPECTED: GitHub should respond with 200 and data.
    // ---> ACTUAL: GitHub server lies, responds with incorrect 304 response, telling client that the cached data is still valid.
    // ---> THE BAD: Client cache believes GitHub - uses the previously cached 404 (and even adds the ETag).
    // ---> THE UGLY: Client is now stuck with a bad cached 404, and can't get rid of it until the resource is _updated_ again or the cache is cleared manually.
    //
    // This is the cause of JENKINS-54126. This is a pervasive GitHub server problem.
    // We see it mostly in this one scenario, but it will happen anywhere the server returns a 404.
    // It cannot be reliably detected or mitigated at the level of this plugin.
    //
    // WORKAROUND (implemented in the github-api library):
    // 4. the github-api library recognizes any 404 with ETag as invalid. Does not return it to the client.
    // ---> The github-api library automatically retries the request with "no-cache" to force refresh with valid data.

    // 1.
    assertFalse(probe.stat("README.md").exists());

    // 3.
    // Without 4. this would return false and would stay false.
    assertTrue(probe.stat("README.md").exists());

    // 5. Verify caching is working
    assertTrue(probe.stat("README.md").exists());

    // Verify the expected requests were made
    if(hudson.Functions.isWindows()) {
        // On windows caching is disabled by default, so the work around doesn't happen
        githubApi.verify(3, RequestPatternBuilder.newRequestPattern(RequestMethod.GET, urlPathEqualTo("/repos/cloudbeers/yolo/contents/"))
            .withHeader("Cache-Control", equalTo("max-age=0"))
            .withHeader("If-Modified-Since", absent())
            .withHeader("If-None-Match", absent())
        );
    } else {
        // 1.
        githubApi.verify(RequestPatternBuilder.newRequestPattern(RequestMethod.GET, urlPathEqualTo("/repos/cloudbeers/yolo/contents/"))
            .withHeader("Cache-Control", equalTo("max-age=0"))
            .withHeader("If-None-Match", absent())
            .withHeader("If-Modified-Since", absent())
        );

        // 3.
        githubApi.verify(RequestPatternBuilder.newRequestPattern(RequestMethod.GET, urlPathEqualTo("/repos/cloudbeers/yolo/contents/"))
            .withHeader("Cache-Control", containing("max-age"))
            .withHeader("If-None-Match", absent())
            .withHeader("If-Modified-Since", containing("GMT"))
        );

        // 4.
        githubApi.verify(RequestPatternBuilder.newRequestPattern(RequestMethod.GET, urlPathEqualTo("/repos/cloudbeers/yolo/contents/"))
            .withHeader("Cache-Control", equalTo("no-cache"))
            .withHeader("If-Modified-Since", absent())
            .withHeader("If-None-Match", absent())
        );

        // 5.
        githubApi.verify(RequestPatternBuilder.newRequestPattern(RequestMethod.GET, urlPathEqualTo("/repos/cloudbeers/yolo/contents/"))
            .withHeader("Cache-Control", equalTo("max-age=0"))
            .withHeader("If-None-Match", equalTo("\"d3be5b35b8d84ef7ac03c0cc9c94ed81\""))
        );
    }
}
 
Example #12
Source File: RequestVerifierFilter.java    From spring-cloud-netflix with Apache License 2.0 4 votes vote down vote up
@Override
public RequestMethod getMethod() {
	return RequestMethod.fromString(request.getMethod());
}
 
Example #13
Source File: WebhookDefinition.java    From wiremock-webhooks-extension with Apache License 2.0 4 votes vote down vote up
public WebhookDefinition withMethod(RequestMethod method) {
    this.method = method;
    return this;
}
 
Example #14
Source File: WebhookDefinition.java    From wiremock-webhooks-extension with Apache License 2.0 4 votes vote down vote up
public RequestMethod getMethod() {
    return method;
}
 
Example #15
Source File: BasicMappingBuilder.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
BasicMappingBuilder(RequestMethod method, UrlPattern urlPattern) {
	this.requestPatternBuilder = new RequestPatternBuilder(method, urlPattern);
}
 
Example #16
Source File: ContractExchangeHandler.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
@Override
public RequestMethod getMethod() {
	return new RequestMethod(this.result.getMethod().name());
}
 
Example #17
Source File: WireMockUtils.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
/**
 * @return All LoggedRequests that wire mock captured.
 */
public static List<LoggedRequest> findAllLoggedRequests() {
    List<LoggedRequest> requests = findAll(
            new RequestPatternBuilder(RequestMethod.ANY, urlMatching(".*")));
    return requests;
}
 
Example #18
Source File: WiremockStyxRequestAdapter.java    From styx with Apache License 2.0 4 votes vote down vote up
@Override
public RequestMethod getMethod() {
    return RequestMethod.fromString(styxRequest.method().name());
}
 
Example #19
Source File: WireMockTestUtils.java    From junit-servers with MIT License 3 votes vote down vote up
/**
 * Verify that a given request has been triggered.
 *
 * @param endpoint Request endpoint.
 * @param method Request method.
 * @param headerName Header name.
 * @param headerValue Header value.
 */
static void assertRequestWithHeader(String endpoint, HttpMethod method, String headerName, String headerValue) {
	UrlPattern urlPattern = urlEqualTo(endpoint);
	RequestMethod rqMethod = new RequestMethod(method.name());
	RequestPatternBuilder rq = new RequestPatternBuilder(rqMethod, urlPattern);
	rq.withHeader(headerName, equalTo(headerValue));
	WireMock.verify(1, rq);
}