@@ -12,6 +12,7 @@ import org.apache.http.client.methods.CloseableHttpResponse
12
12
import org.apache.http.client.methods.HttpUriRequest
13
13
import org.apache.http.entity.ContentType
14
14
import org.apache.http.impl.client.CloseableHttpClient
15
+ import org.apache.http.message.BasicHttpEntityEnclosingRequest
15
16
import org.junit.Before
16
17
import org.junit.Test
17
18
import org.mockito.invocation.InvocationOnMock
@@ -42,7 +43,7 @@ class ProviderClientTest {
42
43
httpClientFactory = [newClient : { provider -> mockHttpClient } ] as IHttpClientFactory
43
44
client = new ProviderClient (provider, httpClientFactory)
44
45
when(mockHttpClient. execute(any())). thenAnswer { InvocationOnMock invocation ->
45
- args = invocation. arguments. first()
46
+ args = invocation. arguments. first() as HttpUriRequest
46
47
[
47
48
getStatusLine : { [getStatusCode : { 200 } ] as StatusLine },
48
49
getAllHeaders : { [] as Header [] },
@@ -69,4 +70,39 @@ class ProviderClientTest {
69
70
assert args. entity. content. text == ' A=B'
70
71
}
71
72
73
+ @Test
74
+ void ' setupBody() needs to take Content-Type headegr into account (UTF-8)' () {
75
+ def contentType = ' text/plain; charset=UTF-8'
76
+ def headers = [' Content-Type' : [contentType]]
77
+ def body = ' ÄÉÌÕÛ'
78
+ def request = new Request (' PUT' , ' /' , [:], headers, OptionalBody . body(body. bytes))
79
+ def method = new BasicHttpEntityEnclosingRequest (' PUT' , ' /' )
80
+ client. setupBody(request, method)
81
+ assert method. entity. contentType. value == contentType
82
+ assert method. entity. content. getText(' UTF-8' ) == body
83
+ }
84
+
85
+ @Test
86
+ void ' setupBody() needs to take Content-Type header into account (ISO-8859-1)' () {
87
+ def contentType = ' text/plain; charset=ISO-8859-1'
88
+ def headers = [' Content-Type' : [contentType]]
89
+ def body = ' ÄÉÌÕÛ'
90
+ def request = new Request (' PUT' , ' /' , [:], headers, OptionalBody . body(body. bytes))
91
+ def method = new BasicHttpEntityEnclosingRequest (' PUT' , ' /' )
92
+ client. setupBody(request, method)
93
+ assert method. entity. contentType. value == contentType
94
+ assert method. entity. content. getText(' ISO-8859-1' ) == body
95
+ }
96
+
97
+ @Test
98
+ void ' setupBody() Content-Type defaults to plain text without encoding' () {
99
+ def contentType = ' text/plain'
100
+ def body = ' ÄÉÌÕÛ'
101
+ def request = new Request (' PUT' , ' /' , [:], [:], OptionalBody . body(body. bytes))
102
+ def method = new BasicHttpEntityEnclosingRequest (' PUT' , ' /' )
103
+ client. setupBody(request, method)
104
+ assert method. entity. contentType. value == contentType
105
+ assert method. entity. content. getText(' ISO-8859-1' ) == body
106
+ }
107
+
72
108
}
0 commit comments