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.57k stars 1.07k forks source link

Error message when I use regex pattern #45

Closed mtakahashi-ivi closed 10 years ago

mtakahashi-ivi commented 10 years ago

Hello,

I'm using mock server ver.2.9. When I use code as following, it works as expected. But mock server prints error log as following. Can I ignore this log?

Error log

3859 [nioEventLoopGroup-3-2] ERROR org.mockserver.matchers.RegexStringMatcher  - Error while matching regex [{"test":"foobar"}] for string [.*"test":".*".*] Illegal repetition
{"test":"foobar"}

Test code

public class MockServerTest {
    private static final String HOST = "localhost";
    private static final int PORT = 9999;
    private static ClientAndServer mockServer;

    @BeforeClass
    public static void beforeClass() {
        mockServer = startClientAndServer(PORT);
    }

    @AfterClass
    public static void afterClass() {
        mockServer.stop();
    }

    @Test
    public void test() throws Throwable {
        final String requestBodyRegExp = ".*\"test\":\".*\".*";
        final String resource = "/test";
        final String responsedBody = "{\"message\":\"foobar\"}";

        new MockServerClient(HOST, PORT)
        .when(
                request()
                        .withMethod("POST")
                        .withPath(resource)
                        .withBody(StringBody.regex(requestBodyRegExp)),
                Times.exactly(1)
        )
        .respond(
                response()
                        .withStatusCode(200)
                        .withHeaders(
                                new Header("Content-Type", "application/json; charset=utf-8")
                        )
                        .withBody(responsedBody)
                        .withDelay(new Delay(TimeUnit.SECONDS, 1))
        );

        URL restServiceURL = new URL("http://"+HOST+":"+PORT+resource);

        HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL
                .openConnection();
        httpConnection.setDoOutput(true);
        httpConnection.setRequestMethod("POST");

        String input = "{\"test\":\"foobar\"}";;
        OutputStream outputStream = httpConnection.getOutputStream();
        outputStream.write(input.getBytes());
        outputStream.flush();

        if (httpConnection.getResponseCode() != 200) {
            throw new RuntimeException(
                    "HTTP GET Request Failed with Error code : "
                            + httpConnection.getResponseCode());
        }

        BufferedReader responseBuffer = new BufferedReader(
                new InputStreamReader((httpConnection.getInputStream())));
        StringBuilder b = new StringBuilder();
        String line;
        while ((line = responseBuffer.readLine()) != null) {
            b.append(line);
        }

        assertThat(b.toString(), is(responsedBody));
    }

}
jamesdbloom commented 10 years ago

Yes the log was at the wrong level. The RegexStringMatcher matches both ways so that either the body is matched against the regex or the match is reversed so that the regex is matched against the body. This is for a number of reasons and significantly improves the robustness however it means that these errors typically occur. I have fixed this and changed the log level down to trace as it is an expected error. I'm going to do a release this fix and a number of other small fixes so you should be able to upgrade to version 3.1 from tomorrow and see the error go away.

mtakahashi-ivi commented 10 years ago

Thank you for your answer and quick fix.