Let mock server write response in UTF-8
This commit is contained in:
parent
dbfb3727af
commit
57a6e06ac0
3 changed files with 45 additions and 6 deletions
|
@ -13,7 +13,7 @@ import org.apache.http.util.EntityUtils
|
||||||
class Util {
|
class Util {
|
||||||
static GPathResult extractXmlResponse(CloseableHttpResponse response) {
|
static GPathResult extractXmlResponse(CloseableHttpResponse response) {
|
||||||
HttpEntity entity = response.entity
|
HttpEntity entity = response.entity
|
||||||
GPathResult xml = new XmlSlurper().parseText(EntityUtils.toString(entity))
|
GPathResult xml = new XmlSlurper().parseText(EntityUtils.toString(entity, 'UTF-8'))
|
||||||
EntityUtils.consumeQuietly(entity)
|
EntityUtils.consumeQuietly(entity)
|
||||||
return xml
|
return xml
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ class Util {
|
||||||
|
|
||||||
static Object extractJsonResponse(CloseableHttpResponse response) {
|
static Object extractJsonResponse(CloseableHttpResponse response) {
|
||||||
HttpEntity entity = response.entity
|
HttpEntity entity = response.entity
|
||||||
Object json = new JsonSlurper().parseText(EntityUtils.toString(entity))
|
Object json = new JsonSlurper().parseText(EntityUtils.toString(entity, 'UTF-8'))
|
||||||
EntityUtils.consumeQuietly(entity)
|
EntityUtils.consumeQuietly(entity)
|
||||||
return json
|
return json
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,29 @@
|
||||||
package pl.touk.mockserver.tests
|
package pl.touk.mockserver.tests
|
||||||
|
|
||||||
import groovy.util.slurpersupport.GPathResult
|
import groovy.util.slurpersupport.GPathResult
|
||||||
import org.apache.http.client.methods.*
|
import org.apache.http.client.methods.CloseableHttpResponse
|
||||||
|
import org.apache.http.client.methods.HttpDelete
|
||||||
|
import org.apache.http.client.methods.HttpGet
|
||||||
|
import org.apache.http.client.methods.HttpHead
|
||||||
|
import org.apache.http.client.methods.HttpOptions
|
||||||
|
import org.apache.http.client.methods.HttpPatch
|
||||||
|
import org.apache.http.client.methods.HttpPost
|
||||||
|
import org.apache.http.client.methods.HttpPut
|
||||||
|
import org.apache.http.client.methods.HttpTrace
|
||||||
import org.apache.http.entity.ContentType
|
import org.apache.http.entity.ContentType
|
||||||
import org.apache.http.entity.StringEntity
|
import org.apache.http.entity.StringEntity
|
||||||
import org.apache.http.impl.client.CloseableHttpClient
|
import org.apache.http.impl.client.CloseableHttpClient
|
||||||
import org.apache.http.impl.client.HttpClients
|
import org.apache.http.impl.client.HttpClients
|
||||||
import org.apache.http.util.EntityUtils
|
import org.apache.http.util.EntityUtils
|
||||||
import pl.touk.mockserver.client.*
|
import pl.touk.mockserver.client.AddMockRequestData
|
||||||
|
import pl.touk.mockserver.client.InvalidMockDefinition
|
||||||
|
import pl.touk.mockserver.client.Method
|
||||||
|
import pl.touk.mockserver.client.MockAlreadyExists
|
||||||
|
import pl.touk.mockserver.client.MockDoesNotExist
|
||||||
|
import pl.touk.mockserver.client.MockEvent
|
||||||
|
import pl.touk.mockserver.client.RegisteredMock
|
||||||
|
import pl.touk.mockserver.client.RemoteMockServer
|
||||||
|
import pl.touk.mockserver.client.Util
|
||||||
import pl.touk.mockserver.server.HttpMockServer
|
import pl.touk.mockserver.server.HttpMockServer
|
||||||
import spock.lang.Shared
|
import spock.lang.Shared
|
||||||
import spock.lang.Specification
|
import spock.lang.Specification
|
||||||
|
@ -52,6 +68,28 @@ class MockServerIntegrationTest extends Specification {
|
||||||
remoteMockServer.removeMock('testRest')?.size() == 1
|
remoteMockServer.removeMock('testRest')?.size() == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def "should add working rest mock on endpoint with utf"() {
|
||||||
|
expect:
|
||||||
|
remoteMockServer.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRestUtf',
|
||||||
|
path: 'testEndpoint',
|
||||||
|
port: 9999,
|
||||||
|
predicate: '''{req -> req.xml.name() == 'request' && req.xml.@test == 'łżźćąś'}''',
|
||||||
|
response: '''{req -> "<goodResponseRest-${req.xml.name()} ans='łżźćąś'/>"}''',
|
||||||
|
soap: false
|
||||||
|
))
|
||||||
|
when:
|
||||||
|
HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint')
|
||||||
|
restPost.entity = new StringEntity('<request test="łżźćąś"/>', ContentType.create("text/xml", "UTF-8"))
|
||||||
|
CloseableHttpResponse response = client.execute(restPost)
|
||||||
|
then:
|
||||||
|
GPathResult restPostResponse = Util.extractXmlResponse(response)
|
||||||
|
restPostResponse.name() == 'goodResponseRest-request'
|
||||||
|
restPostResponse.@ans == 'łżźćąś'
|
||||||
|
expect:
|
||||||
|
remoteMockServer.removeMock('testRestUtf')?.size() == 1
|
||||||
|
}
|
||||||
|
|
||||||
def "should add soap mock on endpoint"() {
|
def "should add soap mock on endpoint"() {
|
||||||
expect:
|
expect:
|
||||||
remoteMockServer.addMock(new AddMockRequestData(
|
remoteMockServer.addMock(new AddMockRequestData(
|
||||||
|
|
|
@ -4,9 +4,10 @@ import com.sun.net.httpserver.HttpExchange
|
||||||
|
|
||||||
class Util {
|
class Util {
|
||||||
static void createResponse(HttpExchange ex, String response, int statusCode) {
|
static void createResponse(HttpExchange ex, String response, int statusCode) {
|
||||||
ex.sendResponseHeaders(statusCode, response ? response.length() : -1)
|
byte[] responseBytes = response ? response.getBytes('UTF-8') : new byte[0]
|
||||||
|
ex.sendResponseHeaders(statusCode, responseBytes.length ?: -1)
|
||||||
if (response) {
|
if (response) {
|
||||||
ex.responseBody << response
|
ex.responseBody << responseBytes
|
||||||
ex.responseBody.close()
|
ex.responseBody.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue