本文整理了Java中org.mockserver.model.HttpRequest
类的一些代码示例,展示了HttpRequest
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest
类的具体详情如下:
包路径:org.mockserver.model.HttpRequest
类名称:HttpRequest
暂无
代码示例来源:origin: jamesdbloom/mockserver
/**
* Clear all expectations and logs that match the http
*
* @param httpRequest the http request that is matched against when deciding whether to clear each expectation if null all expectations are cleared
*/
public MockServerClient clear(HttpRequest httpRequest) {
sendRequest(request().withMethod("PUT").withPath(calculatePath("clear")).withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "", StandardCharsets.UTF_8));
return clientClass.cast(this);
}
代码示例来源:origin: jamesdbloom/mockserver
@Override
public HttpRequest handle(HttpRequest httpRequest) {
return request()
.withHeader("Host", "localhost:" + httpRequest.getFirstHeader("x-echo-server-port"))
.withHeader("x-test", httpRequest.getFirstHeader("x-test"))
.withBody("some_overridden_body")
.withSecure(httpRequest.isSecure());
}
}
代码示例来源:origin: jamesdbloom/mockserver
public HttpRequest update(HttpRequest replaceRequest) {
if (!Strings.isNullOrEmpty(replaceRequest.getMethod().getValue())) {
withMethod(replaceRequest.getMethod());
}
if (!Strings.isNullOrEmpty(replaceRequest.getPath().getValue())) {
withPath(replaceRequest.getPath());
}
for (Header header : replaceRequest.getHeaderList()) {
getHeaders().replaceEntry(header);
}
for (Cookie cookie : replaceRequest.getCookieList()) {
withCookie(cookie);
}
for (Parameter parameter : replaceRequest.getQueryStringParameterList()) {
getQueryStringParameters().replaceEntry(parameter);
}
if (replaceRequest.getBody() != null) {
withBody(replaceRequest.getBody());
}
if (replaceRequest.isSecure() != null) {
withSecure(replaceRequest.isSecure());
}
if (replaceRequest.isKeepAlive() != null) {
withKeepAlive(replaceRequest.isKeepAlive());
}
return this;
}
}
代码示例来源:origin: jamesdbloom/mockserver
public HttpRequestDTO(HttpRequest httpRequest, Boolean not) {
super(not);
if (httpRequest != null) {
method = httpRequest.getMethod();
path = httpRequest.getPath();
headers = httpRequest.getHeaders();
cookies = httpRequest.getCookies();
queryStringParameters = httpRequest.getQueryStringParameters();
body = BodyDTO.createDTO(httpRequest.getBody());
keepAlive = httpRequest.isKeepAlive();
secure = httpRequest.isSecure();
}
}
代码示例来源:origin: jamesdbloom/mockserver
public HttpRequest clone() {
return not(request(), not)
.withMethod(method)
.withPath(path)
.withQueryStringParameters(getQueryStringParameters().clone())
.withBody(body)
.withHeaders(getHeaders().clone())
.withCookies(getCookies().clone())
.withKeepAlive(keepAlive)
.withSecure(secure);
}
代码示例来源:origin: jamesdbloom/mockserver
/**
* Reset MockServer by clearing all expectations
*/
public MockServerClient reset() {
MockServerEventBus.getInstance().publish(EventType.RESET);
sendRequest(request().withMethod("PUT").withPath(calculatePath("reset")));
return clientClass.cast(this);
}
代码示例来源:origin: jamesdbloom/mockserver
request()
.withPath(calculatePath("some_path"))
.withBody("some_request_body"),
makeRequest(
request()
.withPath(calculatePath("some_path"))
.withHeader("name", "value")
.withBody("some_request_body"),
headersToIgnore)
);
.withBody("some_request_body"),
makeRequest(
request()
.withSecure(true)
.withPath(calculatePath("some_path"))
.withHeader("name", "value")
.withBody("some_request_body"),
headersToIgnore)
);
代码示例来源:origin: jamesdbloom/mockserver
request()
.withPath(calculatePath("callback"))
request()
.withPath(calculatePath("callback"))
.withMethod("POST")
.withHeaders(
header("X-Test", "test_headers_and_body")
.withBody("an_example_body_http"),
headersToIgnore)
);
assertEquals(StaticTestExpectationResponseCallback.httpRequests.get(0).getBody().getValue(), "an_example_body_http");
assertEquals(StaticTestExpectationResponseCallback.httpRequests.get(0).getPath().getValue(), calculatePath("callback"));
request()
.withSecure(true)
.withPath(calculatePath("callback"))
.withMethod("POST")
.withHeaders(
header("X-Test", "test_headers_and_body")
.withBody("an_example_body_https"),
headersToIgnore
assertEquals(StaticTestExpectationResponseCallback.httpRequests.get(1).getBody().getValue(), "an_example_body_https");
assertEquals(StaticTestExpectationResponseCallback.httpRequests.get(1).getPath().getValue(), calculatePath("callback"));
代码示例来源:origin: oblac/jodd
@Test
void testDirect() {
HttpResponse response = HttpRequest.get("http://localhost:1080/get_books").send();
assertEquals(200, response.statusCode());
assertTrue(response.body().contains("Tatum"));
proxy.verify(request().withPath("/get_books"), exactly(0));
}
代码示例来源:origin: jamesdbloom/mockserver
request()
.withMethod("POST")
.withPath(calculatePath("some_pathRequest"))
.withQueryStringParameters(
param("queryStringParameterOneName", "queryStringParameterOneValueOne", "queryStringParameterOneValueTwo"),
param("queryStringParameterTwoName", "queryStringParameterTwoValue")
.withBody("some_bodyRequest")
.withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()),
makeRequest(
request()
.withMethod("POST")
.withPath(calculatePath("some_pathRequest"))
.withQueryStringParameters(
param("queryStringParameterOneName", "OTHERqueryStringParameterOneValueOne", "queryStringParameterOneValueTwo"),
param("queryStringParameterTwoName", "queryStringParameterTwoValue")
.withBody("some_bodyRequest")
.withHeaders(header("headerNameRequest", "headerValueRequest"))
.withCookies(cookie("cookieNameRequest", "cookieValueRequest")),
headersToIgnore)
);
代码示例来源:origin: jamesdbloom/mockserver
public HttpRequest buildObject() {
return new HttpRequest()
.withMethod(method)
.withPath(path)
.withQueryStringParameters(queryStringParameters)
.withBody((body != null ? Not.not(body.buildObject(), body.getNot()) : null))
.withHeaders(headers)
.withCookies(cookies)
.withSecure(secure)
.withKeepAlive(keepAlive);
}
代码示例来源:origin: jamesdbloom/mockserver
request()
.withPath("/test_headers_and_body")
.withBody("an_example_body"),
exactly(1)
);
代码示例来源:origin: jamesdbloom/mockserver
protected HttpResponse makeRequest(HttpRequest httpRequest, Collection<String> headersToIgnore) {
try {
boolean isSsl = httpRequest.isSecure() != null && httpRequest.isSecure();
int port = (isSsl ? getServerSecurePort() : getServerPort());
httpRequest.withPath(addContextToPath(httpRequest.getPath().getValue()));
httpRequest.withHeader(HOST.toString(), "localhost:" + port);
boolean isDebug = java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("-agentlib:jdwp") > 0;
HttpResponse httpResponse = httpClient.sendRequest(httpRequest, new InetSocketAddress("localhost", port))
代码示例来源:origin: jamesdbloom/mockserver
@Override
public Long call() throws Exception {
long start = System.currentTimeMillis();
makeRequest(request("/slow"), Collections.<String>emptySet());
return System.currentTimeMillis() - start;
}
});
代码示例来源:origin: jamesdbloom/mockserver
@Override
public HttpResponse call() throws Exception {
return httpClient.sendRequest(
request(addContextToPath(calculatePath("delayed")))
.withHeader(HOST.toString(), "localhost:" + getServerPort())
).get(10, TimeUnit.SECONDS);
}
});
代码示例来源:origin: jamesdbloom/mockserver
@Test
public void shouldReturnResponseByMatchingStringBody() throws IOException, URISyntaxException {
// when
getMockServerClient()
.when(
request()
.withBody(
exact("some_random_body")
),
Times.exactly(2)
)
.respond(
response()
.withBody("some_string_body_response")
);
// then
HttpClient httpClient = createHttpClient();
HttpPost request = new HttpPost(
new URIBuilder()
.setScheme("http")
.setHost("localhost")
.setPort(getServerPort())
.setPath(addContextToPath("some_path"))
.build()
);
request.setEntity(new StringEntity("some_random_body"));
HttpResponse response = httpClient.execute(request);
assertThat(new String(EntityUtils.toByteArray(response.getEntity()), UTF_8), is("some_string_body_response"));
assertThat(response.getStatusLine().getStatusCode(), is(OK_200.code()));
}
代码示例来源:origin: jamesdbloom/mockserver
@Override
public void serialize(HttpRequest httpRequest, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeStartObject();
if (httpRequest.getNot() != null && httpRequest.getNot()) {
jgen.writeBooleanField("not", httpRequest.getNot());
if (httpRequest.getMethod() != null && !Strings.isNullOrEmpty(httpRequest.getMethod().getValue())) {
jgen.writeObjectField("method", httpRequest.getMethod());
if (httpRequest.getPath() != null && !Strings.isNullOrEmpty(httpRequest.getPath().getValue())) {
jgen.writeObjectField("path", httpRequest.getPath());
if (httpRequest.getQueryStringParameterList() != null && !httpRequest.getQueryStringParameterList().isEmpty()) {
jgen.writeObjectField("queryStringParameters", httpRequest.getQueryStringParameters());
if (httpRequest.getHeaderList() != null && !httpRequest.getHeaderList().isEmpty()) {
jgen.writeObjectField("headers", httpRequest.getHeaders());
if (httpRequest.getCookieList() != null && !httpRequest.getCookieList().isEmpty()) {
jgen.writeObjectField("cookies", httpRequest.getCookies());
if (httpRequest.isKeepAlive() != null) {
jgen.writeBooleanField("keepAlive", httpRequest.isKeepAlive());
if (httpRequest.isSecure() != null) {
jgen.writeBooleanField("secure", httpRequest.isSecure());
if (httpRequest.getBody() != null && !Strings.isNullOrEmpty(String.valueOf(httpRequest.getBody().getValue()))) {
jgen.writeObjectField("body", httpRequest.getBody());
代码示例来源:origin: jamesdbloom/mockserver
private void setMethod(HttpRequest httpRequest, HttpServletRequest httpServletRequest) {
httpRequest.withMethod(httpServletRequest.getMethod());
}
代码示例来源:origin: jamesdbloom/mockserver
public HttpRequestTemplateObject(HttpRequest httpRequest) {
if (httpRequest != null) {
method = httpRequest.getMethod().getValue();
path = httpRequest.getPath().getValue();
for (Header header : httpRequest.getHeaderList()) {
headers.put(header.getName().getValue(), Lists.transform(header.getValues(), new Function<NottableString, String>() {
public String apply(NottableString input) {
return input.getValue();
}
}));
}
for (Cookie cookie : httpRequest.getCookieList()) {
cookies.put(cookie.getName().getValue(), cookie.getValue().getValue());
}
for (Parameter parameter : httpRequest.getQueryStringParameterList()) {
queryStringParameters.put(parameter.getName().getValue(), Lists.transform(parameter.getValues(), new Function<NottableString, String>() {
public String apply(NottableString input) {
return input.getValue();
}
}));
}
body = BodyDTO.createDTO(httpRequest.getBody());
keepAlive = httpRequest.isKeepAlive();
secure = httpRequest.isSecure();
}
}
代码示例来源:origin: jamesdbloom/mockserver
/**
* The path to match on such as "/some_mocked_path" any servlet context path is ignored for matching and should not be specified here
* regex values are also supported such as ".*_path", see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
* for full details of the supported regex syntax
*
* @param path the path such as "/some_mocked_path" or a regex
*/
public HttpRequest withPath(String path) {
withPath(string(path));
return this;
}
内容来源于网络,如有侵权,请联系作者删除!