All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.confluex.mock.http.MockHttpServer.groovy Maven / Gradle / Ivy

There is a newer version: 0.4.3
Show newest version
package com.confluex.mock.http

import com.confluex.mock.http.matchers.HttpRequestMatcher
import org.mortbay.jetty.Server

class MockHttpServer {

    int port
    Server jettyServer
    MockHttpRequestHandler handler

    MockHttpServer() {
        this(0)
    }

    MockHttpServer(int port) {
        this.port = port
        if (0 == port) findAvailablePort()
        jettyServer = initJettyServer(this.port)
        jettyServer.handler = handler = new MockHttpRequestHandler()
        jettyServer.start()
    }

    protected Server initJettyServer(int port) {
        new Server(port)
    }

    private void findAvailablePort() {
        def socket = new ServerSocket(0)
        port = socket.getLocalPort()
        socket.close()
    }

    void stop() {
        jettyServer.stop()
    }

    HttpResponderBuilder respondTo(HttpRequestMatcher matcher) {
        handler.respondTo(matcher)
    }

    boolean waitFor(HttpRequestMatcher matcher, Long timeoutMs) {
        waitFor(matcher, 1, timeoutMs)
    }

    boolean waitFor(HttpRequestMatcher matcher, int expected, Long timeoutMs) {
        handler.waitFor(matcher, expected, timeoutMs)
    }

    List getRequests() {
        handler.requests
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy