Add project description
This commit is contained in:
parent
0c36369356
commit
435b56e7b3
5 changed files with 164 additions and 3 deletions
145
README.md
Normal file
145
README.md
Normal file
|
@ -0,0 +1,145 @@
|
|||
# HTTP MOCK SERVER
|
||||
|
||||
## Create server jar
|
||||
|
||||
```
|
||||
mvn clean package assembly:single
|
||||
```
|
||||
|
||||
## Start server on port (default 9999)
|
||||
|
||||
```
|
||||
java -jar mockserver-<VERSION>-jar-with-dependencies.jar [PORT]
|
||||
```
|
||||
|
||||
## Create mock on server via client
|
||||
|
||||
```
|
||||
ControlServerClient controlServerClient = new ControlServerClient('localhost', <PORT>)
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: '...',
|
||||
path: '...',
|
||||
port: ...,
|
||||
predicate: '''...''',
|
||||
response: '''...''',
|
||||
soap: ...,
|
||||
statusCode: ...,
|
||||
method: ...,
|
||||
responseHeaders: ...
|
||||
))
|
||||
```
|
||||
|
||||
or via sending POST request to localhost:<PORT>/serverControl
|
||||
|
||||
|
||||
```
|
||||
<addMock>
|
||||
<name>...</name>
|
||||
<path>...</path>
|
||||
<port>...</port>
|
||||
<predicate>...</predicate>
|
||||
<response>...</response>
|
||||
<soap>...</soap>
|
||||
<statusCode>...</statusCode>
|
||||
<method>...</method>
|
||||
<responseHeaders>,,,</responseHeaders>
|
||||
</addMock>
|
||||
```
|
||||
|
||||
* name - name of mock, must be unique
|
||||
* path - path on which mock should be created
|
||||
* port - inteer, port on which mock should be created, cannot be the same as mock server port
|
||||
* predicate - groovy closure as string which must evaluate to true, when request object will be given to satisfy mock, optional, default {_ -> true}
|
||||
* response - groovy closure as string which must evaluate to string which will be response of mock when predicate is satisfied, optional, default { _ -> '' }
|
||||
* soap - true or false, is request and response should be wrapped in soap Envelope and Body elements, default false
|
||||
* statusCode - integer, status code of response when predicate is satisfied, default 200
|
||||
* method - POST|PUT|DELETE|GET|TRACE|OPTION|HEAD, expected http method of request, default POST
|
||||
* responseHeaders - groovyClosure as string which must evaluate to Map which will be added to response headers, default { _ -> [:] }
|
||||
|
||||
Response if success:
|
||||
|
||||
```
|
||||
<mockAdded/>
|
||||
```
|
||||
|
||||
Response with error message if failure:
|
||||
|
||||
```
|
||||
<exceptionOccured>...</exceptionOccured>
|
||||
```
|
||||
|
||||
## When mock was used it could be unregistered by name. It returns report of mock invocations.
|
||||
Via client:
|
||||
|
||||
```
|
||||
List<MockEvent> mockEvents = controlServerClient.removeMock('...')
|
||||
```
|
||||
|
||||
Via sending POST request to localhost:<PORT>/serverControl
|
||||
|
||||
```
|
||||
<removeMock>
|
||||
<name>...</name>
|
||||
</removeMock>
|
||||
```
|
||||
|
||||
Response if success:
|
||||
|
||||
```
|
||||
<mockRemoved>
|
||||
<mockEvent>
|
||||
<request>
|
||||
<text>...</text>
|
||||
<headers>
|
||||
<param name='...'>...</param>
|
||||
...
|
||||
</headers>
|
||||
<query>
|
||||
<param name='...'>...</param>
|
||||
...
|
||||
</query>
|
||||
<path>
|
||||
<elem>...</elem>
|
||||
...
|
||||
</path>
|
||||
</request>
|
||||
<response>
|
||||
<text>...</text>
|
||||
<headers>
|
||||
<param name='...'>...</param>
|
||||
...
|
||||
</headers>
|
||||
<statusCode>...</statusCode>
|
||||
</response>
|
||||
</mockEvent>
|
||||
...
|
||||
</mockRemoved>
|
||||
```
|
||||
|
||||
Responsewith error message if failure:
|
||||
|
||||
```
|
||||
<exceptionOccured>...</exceptionOccured>
|
||||
```
|
||||
|
||||
## List of current registered mocks could be retrieved:
|
||||
Via client:
|
||||
|
||||
```
|
||||
List<RegisteredMock> mocks = controlServerClient.listMocks()
|
||||
```
|
||||
|
||||
or via sending GET request to localhost:<PORT>/serverControl
|
||||
|
||||
Response:
|
||||
|
||||
```
|
||||
<mocks>
|
||||
<mock>
|
||||
<name>...</name>
|
||||
<path>...</path>
|
||||
<port>...</port>
|
||||
</mock>
|
||||
...
|
||||
</mocks>
|
||||
```
|
|
@ -13,7 +13,6 @@ import org.apache.http.util.EntityUtils
|
|||
class Util {
|
||||
static GPathResult extractXmlResponse(CloseableHttpResponse response) {
|
||||
HttpEntity entity = response.entity
|
||||
|
||||
GPathResult xml = new XmlSlurper().parseText(EntityUtils.toString(entity))
|
||||
EntityUtils.consumeQuietly(entity)
|
||||
return xml
|
||||
|
|
|
@ -26,4 +26,21 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>pl.touk.mockserver.server.Main</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -20,7 +20,7 @@ class HttpServerWraper {
|
|||
InetSocketAddress addr = new InetSocketAddress(Inet4Address.getByName("0.0.0.0"), port)
|
||||
httpServer = HttpServer.create(addr, 0)
|
||||
httpServer.executor = Executors.newCachedThreadPool()
|
||||
log.info("Http server statrting on port $port...")
|
||||
log.info("Http server starting on port $port...")
|
||||
httpServer.start()
|
||||
log.info('Http server is started')
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import groovy.util.logging.Slf4j
|
|||
@Slf4j
|
||||
class Main {
|
||||
static void main(String[] args) {
|
||||
HttpMockServer httpMockServer = new HttpMockServer()
|
||||
HttpMockServer httpMockServer = args.length == 1 ? new HttpMockServer(args[0] as int) : new HttpMockServer()
|
||||
|
||||
Runtime.runtime.addShutdownHook(new Thread({
|
||||
log.info('Http server is stopping...')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue