mock-server / mockserver

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).
http://mock-server.com
Apache License 2.0
4.59k stars 1.07k forks source link

how to use mockserver-war-5.3.0.war #410

Closed anymoreT closed 6 years ago

anymoreT commented 6 years ago
  1. I deploy the war using tomcat.
  2. create a maven project to check this function( org.mockserver.client.server.MockServerClient - makes HTTP requests to a remote MockServer instance)

import org.mockserver.client.server.MockServerClient; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response;

public class MockServerClientExamples { public static void main (String[] args) { new MockServerClient("localhost", 1080) .when( request() .withMethod("GET") .withPath("/view") ) .respond( response() .withBody("+++++++testing!") ); } }

  1. run above code, get below prompt: 22:20:14.927 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8 Exception in thread "main" org.mockserver.client.ClientException:

error:

<!doctype html><html lang="en"><head><title>HTTP Status 403 – Forbidden</title><style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 403 – Forbidden</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The server understood the request but refuses to authorize it.</p><hr class="line" /><h3>Apache Tomcat/8.5.24</h3></body></html>

while submitted expectation:

{
  "httpRequest" : {
    "method" : "GET",
    "path" : "/view"
  },
  "times" : {
    "unlimited" : true
  },
  "timeToLive" : {
    "unlimited" : true
  },
  "httpResponse" : {
    "body" : "+++++++testing!"
  }
}

at org.mockserver.client.AbstractClient.sendExpectation(AbstractClient.java:444)
at org.mockserver.client.ForwardChainExpectation.respond(ForwardChainExpectation.java:25)
at org.test.MockServerClientExamples.main(MockServerClientExamples.java:22)

who can tell me how to use the war? My requirement is to create a mockserver that can provide some customized response from the outside.

anymoreT commented 6 years ago

Our front-end team needs a server that can mock all requests.

anymoreT commented 6 years ago

I also have tried to compile the examples to a war, and deploy the war to tomcat. I think I don't understand how to use mockserver.

jamesdbloom commented 6 years ago

I think the main issue is with your instance of Tomcat, the HTML returned by Tomcat includes: The server understood the request but refuses to authorize it.. This is a Tomcat authorisation error and not related to MockServer.

Your tomcat is preventing access to the deployed MockServer WAR.

You can not use one of the other mechanisms to run MockServer, as listed at: http://mock-server.com/mock_server/getting_started.html#start_mockserver

For example can you run MockServer from the command line, using the downloaded Jar, maven command or docker image?

anymoreT commented 6 years ago

thank you very much. I will try it .