HttpForwardActionResult notFoundFuture(HttpRequest httpRequest) { SettableFuture<HttpResponse> notFoundFuture = SettableFuture.create(); notFoundFuture.set(notFoundResponse()); return new HttpForwardActionResult(httpRequest, notFoundFuture); } }
@Override public HttpResponse handle(HttpRequest httpRequest) { if (httpRequest.getPath().getValue().endsWith("/callback")) { return httpResponse; } else { return notFoundResponse(); } } }
private HttpResponse invokeCallbackMethod(HttpClassCallback httpClassCallback, HttpRequest httpRequest) { if (httpRequest != null) { ExpectationResponseCallback expectationResponseCallback = instantiateCallback(httpClassCallback); if (expectationResponseCallback != null) { try { return expectationResponseCallback.handle(httpRequest); } catch (Throwable throwable) { mockServerLogger.error(httpClassCallback.getCallbackClass() + " throw exception while executing handle callback method", throwable); return notFoundResponse(); } } else { return notFoundResponse(); } } else { return notFoundResponse(); } } }
private void returnNotFound(ResponseWriter responseWriter, HttpRequest request) { HttpResponse response = notFoundResponse(); if (request.getHeaders().containsEntry("x-forwarded-by", "MockServer")) { response.withHeader("x-forwarded-by", "MockServer"); mockServerLogger.trace(request, "no expectation for:{}returning response:{}", request, notFoundResponse()); } else { httpStateHandler.log(new RequestLogEntry(request)); mockServerLogger.info(EXPECTATION_NOT_MATCHED, request, "no expectation for:{}returning response:{}", request, notFoundResponse()); } responseWriter.writeResponse(request, response, false); } }
public HttpResponse handle(HttpTemplate httpTemplate, HttpRequest httpRequest) { HttpResponse httpResponse = notFoundResponse(); TemplateEngine templateEngine = null; switch (httpTemplate.getTemplateType()) { case VELOCITY: templateEngine = velocityTemplateEngine; break; case JAVASCRIPT: templateEngine = javaScriptTemplateEngine; break; default: throw new RuntimeException("Unknown no template engine available for " + httpTemplate.getTemplateType()); } if (templateEngine != null) { HttpResponse templatedResponse = templateEngine.executeTemplate(httpTemplate.getTemplate(), httpRequest, HttpResponseDTO.class); if (templatedResponse != null) { return templatedResponse; } } return httpResponse; }
); assertEquals( notFoundResponse(), makeRequest( request().withPath(calculatePath("not_found")),
public void renderDashboard(final ChannelHandlerContext ctx, final HttpRequest request) throws Exception { HttpResponse response = notFoundResponse(); if (request.getMethod().getValue().equals("GET")) { String path = StringUtils.substringAfter(request.getPath().getValue(), PATH_PREFIX + "/dashboard");
public void run() { try { HttpResponse response = responseFuture.getHttpResponse().get(); if (response == null) { response = notFoundResponse(); } if (response.containsHeader("x-forwarded-by", "MockServer")) { httpStateHandler.log(new RequestLogEntry(request)); mockServerLogger.info(EXPECTATION_NOT_MATCHED, request, "no expectation for:{}returning response:{}", request, notFoundResponse()); } else { httpStateHandler.log(new RequestResponseLogEntry(request, response)); mockServerLogger.info(FORWARDED_REQUEST, request, "returning response:{}for forwarded request" + NEW_LINE + NEW_LINE + " in json:{}" + NEW_LINE + NEW_LINE + " in curl:{}", response, request, httpRequestToCurlSerializer.toCurl(request, remoteAddress)); } responseWriter.writeResponse(request, response, false); } catch (SocketCommunicationException sce) { returnNotFound(responseWriter, request); } catch (Exception ex) { if (exploratoryHttpProxy && (ex.getCause() instanceof ConnectException || ex.getCause() instanceof SocketConnectionException)) { mockServerLogger.trace("Failed to connect to proxied socket due to exploratory HTTP proxy for: {}falling back to no proxy: {}", request, ex.getCause()); returnNotFound(responseWriter, request); } else { mockServerLogger.error(request, ex, ex.getMessage()); } } } }, synchronous);
@Override public void writeResponse(HttpRequest request, HttpResponse response, boolean apiResponse) { if (response == null) { response = notFoundResponse(); } if (enableCORSForAllResponses()) { addCORSHeaders.addCORSHeaders(request, response); } else if (apiResponse && enableCORSForAPI()) { addCORSHeaders.addCORSHeaders(request, response); } if (apiResponse) { response.withHeader("version", org.mockserver.Version.getVersion()); final String path = request.getPath().getValue(); if (!path.startsWith(PATH_PREFIX)) { response.withHeader("deprecated", "\"" + path + "\" is deprecated use \"" + PATH_PREFIX + path + "\" instead"); } } addConnectionHeader(request, response); writeAndCloseSocket(ctx, request, response); }
@Override public void writeResponse(HttpRequest request, HttpResponse response, boolean apiResponse) { if (response == null) { response = notFoundResponse(); } if (enableCORSForAllResponses()) { addCORSHeaders.addCORSHeaders(request, response); } else if (apiResponse && enableCORSForAPI()) { addCORSHeaders.addCORSHeaders(request, response); } if (apiResponse) { response.withHeader("version", org.mockserver.Version.getVersion()); final String path = request.getPath().getValue(); if (!path.startsWith(PATH_PREFIX)) { response.withHeader("deprecated", "\"" + path + "\" is deprecated use \"" + PATH_PREFIX + path + "\" instead"); } } addConnectionHeader(request, response); mockServerResponseToHttpServletResponseEncoder.mapMockServerResponseToHttpServletResponse(response, httpServletResponse); }
); assertEquals( notFoundResponse(), makeRequest( request().withPath(calculatePath("not_found")),
notFoundResponse(), makeRequest( request() notFoundResponse(), makeRequest( request()
notFoundResponse(), makeRequest( request() notFoundResponse(), makeRequest( request()
@Test public void shouldVerifySequenceOfRequestsReceivedIncludingThoseNotMatchingAnException() { // when mockServerClient.when(request().withPath(calculatePath("some_path.*")), exactly(4)).respond(response().withBody("some_body")); // then assertEquals( response("some_body"), makeRequest( request().withPath(calculatePath("some_path_one")), headersToIgnore) ); assertEquals( notFoundResponse(), makeRequest( request().withPath(calculatePath("not_found")), headersToIgnore) ); assertEquals( response("some_body"), makeRequest( request().withPath(calculatePath("some_path_three")), headersToIgnore) ); mockServerClient.verify(request(calculatePath("some_path_one")), request(calculatePath("some_path_three"))); mockServerClient.verify(request(calculatePath("some_path_one")), request(calculatePath("not_found"))); mockServerClient.verify(request(calculatePath("some_path_one")), request(calculatePath("not_found")), request(calculatePath("some_path_three"))); mockServerClient.verify(request(calculatePath("some_path_one")), request(calculatePath("not_found")), request(calculatePath("some_path_three"))); }
); assertEquals( notFoundResponse(), makeRequest( request().withPath(calculatePath("not_found")),
); assertEquals( notFoundResponse(), makeRequest( request().withPath(calculatePath("not_found")),
HttpForwardActionResult notFoundFuture(HttpRequest httpRequest) { SettableFuture<HttpResponse> notFoundFuture = SettableFuture.create(); notFoundFuture.set(notFoundResponse()); return new HttpForwardActionResult(httpRequest, notFoundFuture); } }
private HttpResponse invokeCallbackMethod(HttpClassCallback httpClassCallback, HttpRequest httpRequest) { if (httpRequest != null) { ExpectationResponseCallback expectationResponseCallback = instantiateCallback(httpClassCallback); if (expectationResponseCallback != null) { return expectationResponseCallback.handle(httpRequest); } else { return notFoundResponse(); } } else { return notFoundResponse(); } } }
private void returnNotFound(ResponseWriter responseWriter, HttpRequest request) { HttpResponse response = notFoundResponse(); if (request.getHeaders().containsEntry("x-forwarded-by", "MockServer")) { response.withHeader("x-forwarded-by", "MockServer"); } else { httpStateHandler.log(new RequestLogEntry(request)); mockServerLogger.info(EXPECTATION_NOT_MATCHED, request, "no expectation for:{}returning response:{}", request, notFoundResponse()); } responseWriter.writeResponse(request, response, false); } }
@Test void checkNonExistentUrl() { server.when(HttpRequest.request()).respond(HttpResponse.notFoundResponse()); final UpdateMonitor v = new UpdateMonitor(serverUrl, "com.github.robozonky", "robozonky-nonexistent"); v.run(); assertThat(v.get()).isEmpty(); }