-
Notifications
You must be signed in to change notification settings - Fork 17
/
message.clj
275 lines (182 loc) · 6.36 KB
/
message.clj
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
(ns finagle-clojure.http.message
"Functions for working with [[com.twitter.finagle.http.Message]] and its concrete subclasses,
[[com.twitter.finagle.http.Request]] and [[com.twitter.finagle.http.Response]].
`Request` objects are passed to services bound to a Finagle HTTP server, and `Response` objects must be passed back
(wrapped in a `Future`) in turn. Most requests are constructed by Finagle, but the functions here to may be helpful
to create `MockRequest`s for service testing purposes."
(:import (com.twitter.finagle.http Response Request Status Method Method$ Message ParamMap HeaderMap)
(java.io InputStream)
(java.util Map$Entry))
(:require [finagle-clojure.options :as opt]
[finagle-clojure.scala :as s]))
(defn- ^Status int->Status [c]
(Status/fromCode (int c)))
(defn- ^Method str->Method [m]
(.apply Method$/MODULE$ (-> m (name) (.toUpperCase))))
(defn ^Response response
"Constructs a `Response`, required for Finagle services that interact with an `HttpServer`.
*Arguments*:
* `code` (optional): a number representing the desired HTTP status code of the response
*Returns*:
an instance of [[com.twitter.finagle.http.Response]]"
([]
(Response/apply))
([code]
(Response/apply (int->Status code))))
(defn ^Request request
"Constructs a `Request`. Usually this will be constructed on your behalf for incoming requests;
this function is useful primarily testing purposes, and indeed returns a `MockRequest` in its current form.
*Arguments*:
* `uri`: the URI of the request
* `method` (optional): a keyword or string of the desired HTTP method
*Returns*:
an instance of [[com.twitter.finagle.http.Request]], specifically a MockRequest"
([^String uri]
(Request/apply uri))
([^String uri method]
(Request/apply (str->Method method) uri)))
(defn ^Response set-status-code
"Sets the status code of the given response.
*Arguments*:
* `resp`: a [[com.twitter.finagle.http.Response]]
* `code`: a number with the desired HTTP status code
*Returns*:
the given response"
[^Response resp code]
(.setStatusCode resp code)
resp)
(defn status-code
"Returns the status code of the given `Response`.
*Arguments*:
* `resp`: a [[com.twitter.finagle.http.Response]]
*Returns*:
the status code of the response as an int"
[^Response resp]
(.statusCode resp))
(defn ^Message set-content-string
"Sets the content string of the given message.
*Arguments*:
* `msg`: a [[com.twitter.finagle.http.Message]]
* `content`: a string of content
*Returns*:
the given message"
[^Message msg content]
(.setContentString msg content)
msg)
(defn ^String content-string
"Gets the content string of the given message.
*Arguments*:
* `msg`: a [[com.twitter.finagle.http.Message]]
*Returns*:
the content string of the message"
[^Message msg]
(.contentString msg))
(defn ^Message set-content-type
"Sets the content type of the given message.
*Arguments*:
* `msg`: a [[com.twitter.finagle.http.Message]]
* `type`: a string containing the message's content-type
* `charset` (optional, default: `utf-8`): the charset of the content
*Returns*:
the given message"
([^Message msg type]
(set-content-type msg type "utf-8"))
([^Message msg type charset]
(.setContentType msg type charset)
msg))
(defn ^String content-type
"Gets the content type of the given message.
*Arguments*:
* `msg`: a [[com.twitter.finagle.http.Message]]
*Returns*:
the content type of the message"
[^Message msg]
(opt/get (.contentType msg)))
(defn ^Message set-charset
"Sets the charset of the given message.
*Arguments*:
* `msg`: a [[com.twitter.finagle.http.Message]]
* `charset`: a string charset
*Returns*:
the given message"
[^Message msg charset]
(.charset_$eq msg charset)
msg)
(defn ^String charset
"Gets the charset of the given message.
*Arguments*:
* `msg`: a [[com.twitter.finagle.http.Message]]
*Returns*:
the charset of the message"
[^Message msg]
(opt/get (.charset msg)))
(defn ^Request set-http-method
"Sets the HTTP method of the given request.
*Arguments*:
* `req`: a [[com.twitter.finagle.http.Request]]
* `meth`: a string or keyword containing a valid HTTP method
*Returns*:
the given request"
[^Request req meth]
(.method_$eq req (str->Method meth))
req)
(defn ^String http-method
"Gets the HTTP method of the given request.
*Arguments*:
* `req`: a [[com.twitter.finagle.http.Request]]
*Returns*:
the HTTP method of the request as an uppercase string"
[^Request req]
(-> req (.method) (.toString)))
(defn ^Message set-header
"Sets the named header in the given message.
*Arguments*:
* `msg`: a [[com.twitter.finagle.http.Message]]
* `name`: a string containing the header name
* `value`: a stringable object containing the value
*Returns*:
the given message"
[^Message msg ^String name ^String value]
(.add (.headerMap msg) name value)
msg)
(defn header
"Gets the named header from the given message.
*Arguments*:
* `msg`: a [[com.twitter.finagle.http.Message]]
* `header`: a string containing the header name
*Returns*:
A seq of strings that are the values for the named header in the given message"
[^Message msg ^String header]
(s/scala-seq->vec (.getAll (.headerMap msg) header)))
(defn headers
"Returns this message's headers as a Clojure map.
*Arguments*:
* `msg`: a [[com.twitter.finagle.http.Message]]
*Returns*:
this request's headers as a Clojure map"
[^Message msg]
(s/scala-map->map (.headerMap msg)))
(defn ^InputStream input-stream
"Returns this message's content as an input stream.
*Arguments*:
* `msg`: a [[com.twitter.finagle.http.Message]]
*Returns*:
this request's content as an input stream"
[^Message msg]
(.getInputStream msg))
(defn params
"Returns this request's params as a Clojure map.
*Arguments*:
* `req`: a [[com.twitter.finagle.http.Request]]
*Returns*:
this request's params as a Clojure map"
[^Request req]
(s/scala-map->map (.params req)))
(defn ^String param
"Returns the named param from the given request.
*Arguments*:
* `req`: a [[com.twitter.finagle.http.Request]]
*Returns*:
the string contents of the named param"
[^Request req param]
(.getParam req param))