-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathRatpackAsyncHttpServerTest.groovy
More file actions
234 lines (225 loc) · 8 KB
/
RatpackAsyncHttpServerTest.groovy
File metadata and controls
234 lines (225 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package server
import datadog.appsec.api.blocking.Blocking
import datadog.trace.agent.test.base.HttpServer
import datadog.trace.agent.test.base.HttpServerTest
import io.netty.buffer.ByteBuf
import org.reactivestreams.Subscriber
import org.reactivestreams.Subscription
import ratpack.exec.Promise
import ratpack.form.Form
import ratpack.groovy.test.embed.GroovyEmbeddedApp
import ratpack.handling.HandlerDecorator
import static ratpack.jackson.Jackson.json
import java.nio.charset.StandardCharsets
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.BODY_JSON
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.BODY_MULTIPART
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.BODY_URLENCODED
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.CREATED
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.ERROR
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.EXCEPTION
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.FORWARDED
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.PATH_PARAM
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.QUERY_ENCODED_BOTH
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.QUERY_ENCODED_QUERY
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.QUERY_PARAM
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.REDIRECT
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.SUCCESS
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.USER_BLOCK
class RatpackAsyncHttpServerTest extends RatpackHttpServerTest {
@Override
HttpServer server() {
return new RatpackServer(GroovyEmbeddedApp.ratpack {
serverConfig {
port 0
address InetAddress.getByName('localhost')
}
bindings {
bind TestErrorHandler
multiBindInstance(HandlerDecorator, HandlerDecorator.prepend(new ResponseHeaderDecorator()))
}
handlers {
prefix(SUCCESS.relativeRawPath()) {
all {
Promise.sync {
SUCCESS
} then { HttpServerTest.ServerEndpoint endpoint ->
controller(endpoint) {
context.response.status(endpoint.status).send(endpoint.body)
}
}
}
}
prefix(CREATED.relativeRawPath()) {
all {
Promise.sync {
CREATED
} then {endpoint ->
controller(endpoint) {
def outerDelegate = delegate
request.bodyStream.subscribe(new Subscriber<ByteBuf>() {
Subscription sub
String res = ''
@Override
void onSubscribe(Subscription s) {
sub = s
s.request(1)
}
@Override
void onNext(ByteBuf byteBuf) {
Promise.async {downstream ->
CharSequence sequence =
byteBuf.readCharSequence(byteBuf.readableBytes(), StandardCharsets.UTF_8)
res += sequence
downstream.success(sequence)
} then {
byteBuf.release()
sub.request(1)
}
}
@Override
void onError(Throwable t) {
outerDelegate.error(t)
}
@Override
void onComplete() {
outerDelegate.response.status(endpoint.status)
.send('text/plain', "${endpoint.body}: $res")
}
})
}
}
}
}
prefix(FORWARDED.relativeRawPath()) {
all {
Promise.sync {
FORWARDED
} then { HttpServerTest.ServerEndpoint endpoint ->
controller(endpoint) {
context.response.status(endpoint.status).send(request.headers.get("x-forwarded-for"))
}
}
}
}
get('path/:id/param') {
Promise.sync {
PATH_PARAM
} then {endpoint ->
controller(endpoint) {
context.response.status(PATH_PARAM.status).send('text/plain', context.pathTokens['id'])
}
}
}
[BODY_URLENCODED, BODY_MULTIPART].each { endpoint ->
prefix(endpoint.relativeRawPath()) {
all {
Promise.sync {
endpoint
} then { ep ->
controller(endpoint) {
context.parse(Form).then { form ->
def text = form.findAll { it.key != 'ignore' }
.collectEntries { [it.key, it.value as List] } as String
response.status(endpoint.status).send('text/plain', text)
}
}
}
}
}
}
prefix(BODY_JSON.relativeRawPath()) {
all {
Promise.sync {
BODY_JSON
} then {endpoint ->
controller(endpoint) {
context.parse(Map).then { map ->
response.status(BODY_JSON.status)
context.render(json(map))
}
}
}
}
}
prefix(QUERY_ENCODED_BOTH.relativeRawPath()) {
all {
Promise.sync {
QUERY_ENCODED_BOTH
} then { HttpServerTest.ServerEndpoint endpoint ->
controller(endpoint) {
context.response.status(endpoint.status).send(endpoint.bodyForQuery(request.query))
}
}
}
}
prefix(QUERY_ENCODED_QUERY.relativeRawPath()) {
all {
Promise.sync {
QUERY_ENCODED_QUERY
} then { HttpServerTest.ServerEndpoint endpoint ->
controller(endpoint) {
context.response.status(endpoint.status).send(endpoint.bodyForQuery(request.query))
}
}
}
}
prefix(QUERY_PARAM.relativeRawPath()) {
all {
Promise.sync {
QUERY_PARAM
} then { HttpServerTest.ServerEndpoint endpoint ->
controller(endpoint) {
context.response.status(endpoint.status).send(endpoint.bodyForQuery(request.query))
}
}
}
}
prefix(USER_BLOCK.relativeRawPath()) {
all {
Promise.sync {
USER_BLOCK
} then { HttpServerTest.ServerEndpoint endpoint ->
controller(endpoint) {
Blocking.forUser('user-to-block').blockIfMatch()
context.response.status(SUCCESS.status).send('should never be reached')
}
}
}
}
prefix(REDIRECT.relativeRawPath()) {
all {
Promise.sync {
REDIRECT
} then { HttpServerTest.ServerEndpoint endpoint ->
controller(endpoint) {
context.redirect(endpoint.body)
}
}
}
}
prefix(ERROR.relativeRawPath()) {
all {
Promise.sync {
ERROR
} then { HttpServerTest.ServerEndpoint endpoint ->
controller(endpoint) {
context.response.status(endpoint.status).send(endpoint.body)
}
}
}
}
prefix(EXCEPTION.relativeRawPath()) {
all {
Promise.sync {
EXCEPTION
} then { HttpServerTest.ServerEndpoint endpoint ->
controller(endpoint) {
throw new Exception(endpoint.body)
}
}
}
}
}
})
}
}