onlyoffice-docs_integration_sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. checksums.yaml +7 -0
  2. data/lib/onlyoffice/docs_integration_sdk/document_editor/config.rb +1479 -0
  3. data/lib/onlyoffice/docs_integration_sdk/document_editor/config_test.rb +1713 -0
  4. data/lib/onlyoffice/docs_integration_sdk/document_editor.rb +29 -0
  5. data/lib/onlyoffice/docs_integration_sdk/document_server/client/command.rb +417 -0
  6. data/lib/onlyoffice/docs_integration_sdk/document_server/client/command_test.rb +672 -0
  7. data/lib/onlyoffice/docs_integration_sdk/document_server/client/conversion.rb +477 -0
  8. data/lib/onlyoffice/docs_integration_sdk/document_server/client/conversion_test.rb +682 -0
  9. data/lib/onlyoffice/docs_integration_sdk/document_server/client/healthcheck.rb +101 -0
  10. data/lib/onlyoffice/docs_integration_sdk/document_server/client/healthcheck_test.rb +209 -0
  11. data/lib/onlyoffice/docs_integration_sdk/document_server/client/jwt.rb +116 -0
  12. data/lib/onlyoffice/docs_integration_sdk/document_server/client/jwt_test.rb +70 -0
  13. data/lib/onlyoffice/docs_integration_sdk/document_server/client/response.rb +73 -0
  14. data/lib/onlyoffice/docs_integration_sdk/document_server/client/response_test.rb +49 -0
  15. data/lib/onlyoffice/docs_integration_sdk/document_server/client/service.rb +44 -0
  16. data/lib/onlyoffice/docs_integration_sdk/document_server/client/ua.rb +31 -0
  17. data/lib/onlyoffice/docs_integration_sdk/document_server/client/ua_test.rb +35 -0
  18. data/lib/onlyoffice/docs_integration_sdk/document_server/client.rb +321 -0
  19. data/lib/onlyoffice/docs_integration_sdk/document_server/client_test.rb +1259 -0
  20. data/lib/onlyoffice/docs_integration_sdk/document_server.rb +29 -0
  21. data/lib/onlyoffice/docs_integration_sdk/document_storage/callback.rb +276 -0
  22. data/lib/onlyoffice/docs_integration_sdk/document_storage/callback_test.rb +291 -0
  23. data/lib/onlyoffice/docs_integration_sdk/document_storage.rb +29 -0
  24. data/lib/onlyoffice/docs_integration_sdk/jwt.rb +448 -0
  25. data/lib/onlyoffice/docs_integration_sdk/jwt_test.rb +598 -0
  26. data/lib/onlyoffice/docs_integration_sdk/test_test.rb +113 -0
  27. data/lib/onlyoffice/docs_integration_sdk/version.rb +26 -0
  28. data/lib/onlyoffice/docs_integration_sdk/version_test.rb +33 -0
  29. data/lib/onlyoffice/docs_integration_sdk.rb +31 -0
  30. data/lib/onlyoffice.rb +21 -0
  31. metadata +283 -0
@@ -0,0 +1,1259 @@
1
+ #
2
+ # (c) Copyright Ascensio System SIA 2025
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ # typed: true
18
+ # frozen_string_literal: true
19
+
20
+ require "stringio"
21
+ require "test/unit"
22
+ require_relative "client"
23
+
24
+ module Onlyoffice
25
+ module DocsIntegrationSdk
26
+ module Test
27
+ module DocumentServer
28
+ module Client
29
+ extend T::Sig
30
+ include ::Test::Unit::Assertions
31
+
32
+ sig {returns(T::Array[[String, T.class_of(Net::HTTPRequest)]])}
33
+ def http_methods
34
+ [
35
+ ["DELETE", Net::HTTP::Delete],
36
+ ["GET", Net::HTTP::Get],
37
+ ["HEAD", Net::HTTP::Head],
38
+ ["OPTIONS", Net::HTTP::Options],
39
+ ["PATCH", Net::HTTP::Patch],
40
+ ["POST", Net::HTTP::Post],
41
+ ["PUT", Net::HTTP::Put],
42
+ ["TRACE", Net::HTTP::Trace],
43
+ ]
44
+ end
45
+
46
+ sig {params(m: String, u: URI::HTTP, req: Net::HTTPRequest).void}
47
+ def check_request_basics(m, u, req)
48
+ assert_equal(m, req.method)
49
+ assert_equal(u, req.uri)
50
+ end
51
+
52
+ sig {params(m: String, u: URI::HTTP, req: Net::HTTPRequest).void}
53
+ def check_request_headers(m, u, req)
54
+ h = {
55
+ "accept" => ["application/json"],
56
+ "accept-encoding" => ["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
57
+ "host" => ["#{u.host}:#{u.port}"],
58
+ "user-agent" => [DocsIntegrationSdk::DocumentServer::Client::USER_AGENT],
59
+ }
60
+
61
+ if RUBY_VERSION < "3.0.0" && m == "HEAD"
62
+ h.delete("accept-encoding")
63
+ end
64
+
65
+ assert_equal(h, req.to_hash)
66
+ end
67
+
68
+ sig {params(m: String, u: URI::HTTP, a: String, req: Net::HTTPRequest).void}
69
+ def check_request_headers_with_custom_user_agent(m, u, a, req)
70
+ h = {
71
+ "accept" => ["application/json"],
72
+ "accept-encoding" => ["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
73
+ "host" => ["#{u.host}:#{u.port}"],
74
+ "user-agent" => [a],
75
+ }
76
+
77
+ if RUBY_VERSION < "3.0.0" && m == "HEAD"
78
+ h.delete("accept-encoding")
79
+ end
80
+
81
+ assert_equal(h, req.to_hash)
82
+ end
83
+
84
+ sig {params(m: String, u: URI::HTTP, req: Net::HTTPRequest).void}
85
+ def check_request_headers_with_content_type(m, u, req)
86
+ h = {
87
+ "accept" => ["application/json"],
88
+ "accept-encoding" => ["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
89
+ "content-type" => ["application/json"],
90
+ "host" => ["#{u.host}:#{u.port}"],
91
+ "user-agent" => [DocsIntegrationSdk::DocumentServer::Client::USER_AGENT],
92
+ }
93
+
94
+ if RUBY_VERSION < "3.0.0" && m == "HEAD"
95
+ h.delete("accept-encoding")
96
+ end
97
+
98
+ assert_equal(h, req.to_hash)
99
+ end
100
+
101
+ sig {params(m: String, u: URI::HTTP, a: String, req: Net::HTTPRequest).void}
102
+ def check_request_headers_with_content_type_and_custom_user_agent(m, u, a, req)
103
+ h = {
104
+ "accept" => ["application/json"],
105
+ "accept-encoding" => ["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
106
+ "content-type" => ["application/json"],
107
+ "host" => ["#{u.host}:#{u.port}"],
108
+ "user-agent" => [a],
109
+ }
110
+
111
+ if RUBY_VERSION < "3.0.0" && m == "HEAD"
112
+ h.delete("accept-encoding")
113
+ end
114
+
115
+ assert_equal(h, req.to_hash)
116
+ end
117
+
118
+ sig {params(m: String, u: URI::HTTP, w: JwtDecoding, b: T.untyped, req: Net::HTTPRequest).void}
119
+ def check_request_headers_with_jwt(m, u, w, b, req)
120
+ x = {
121
+ "accept" => ["application/json"],
122
+ "accept-encoding" => ["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
123
+ "content-type" => ["application/json"],
124
+ "host" => ["#{u.host}:#{u.port}"],
125
+ "user-agent" => [DocsIntegrationSdk::DocumentServer::Client::USER_AGENT],
126
+ }
127
+
128
+ if RUBY_VERSION < "3.0.0" && m == "HEAD"
129
+ x.delete("accept-encoding")
130
+ end
131
+
132
+ y = req.to_hash
133
+
134
+ r = y["authorization"]
135
+ assert_equal(1, r.length)
136
+ assert_true(r[0].start_with?("Bearer "))
137
+
138
+ y.delete("authorization")
139
+ assert_equal(x, y)
140
+
141
+ t = r[0].sub("Bearer ", "")
142
+ assert_equal(b, w.decode_header(t))
143
+ end
144
+
145
+ sig {params(m: String, u: URI::HTTP, w: JwtDecoding, h: String, b: T::Hash[String, T.untyped], req: Net::HTTPRequest).void}
146
+ def check_request_headers_with_custom_jwt_header(m, u, w, h, b, req)
147
+ x = {
148
+ "accept" => ["application/json"],
149
+ "accept-encoding" => ["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
150
+ "content-type" => ["application/json"],
151
+ "host" => ["#{u.host}:#{u.port}"],
152
+ "user-agent" => [DocsIntegrationSdk::DocumentServer::Client::USER_AGENT],
153
+ }
154
+
155
+ if RUBY_VERSION < "3.0.0" && m == "HEAD"
156
+ x.delete("accept-encoding")
157
+ end
158
+
159
+ y = req.to_hash
160
+
161
+ r = y[h.downcase]
162
+ assert_equal(1, r.length)
163
+ assert_true(r[0].start_with?("Bearer "))
164
+
165
+ y.delete(h.downcase)
166
+ assert_equal(x, y)
167
+
168
+ t = r[0].sub("Bearer ", "")
169
+ assert_equal(b, w.decode_header(t))
170
+ end
171
+
172
+ sig {params(m: String, u: URI::HTTP, w: JwtDecoding, s: String, b: T::Hash[String, T.untyped], req: Net::HTTPRequest).void}
173
+ def check_request_headers_with_custom_jwt_schema(m, u, w, s, b, req)
174
+ x = {
175
+ "accept" => ["application/json"],
176
+ "accept-encoding" => ["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
177
+ "content-type" => ["application/json"],
178
+ "host" => ["#{u.host}:#{u.port}"],
179
+ "user-agent" => [DocsIntegrationSdk::DocumentServer::Client::USER_AGENT],
180
+ }
181
+
182
+ if RUBY_VERSION < "3.0.0" && m == "HEAD"
183
+ x.delete("accept-encoding")
184
+ end
185
+
186
+ y = req.to_hash
187
+
188
+ r = y["authorization"]
189
+ assert_equal(1, r.length)
190
+ assert_true(r[0].start_with?("#{s} "))
191
+
192
+ y.delete("authorization")
193
+ assert_equal(x, y)
194
+
195
+ t = r[0].sub("#{s} ", "")
196
+ assert_equal(b, w.decode_header(t))
197
+ end
198
+
199
+ sig {params(w: JwtDecoding, b: T::Hash[String, T.untyped], req: Net::HTTPRequest).void}
200
+ def check_request_body_with_jwt(w, b, req)
201
+ h = T.let(JSON.parse(req.body), T::Hash[String, T.untyped])
202
+ assert_equal(b, w.decode_body(h))
203
+ end
204
+
205
+ sig {params(b: String).returns(Net::HTTPResponse)}
206
+ def create_ok(b)
207
+ raw =
208
+ "HTTP/1.1 200 OK\r\n" +
209
+ "Content-Type: application/json; charset=utf-8\r\n" +
210
+ "\r\n" +
211
+ b
212
+ sock = ::StringIO.new(raw)
213
+ buf = Net::BufferedIO.new(sock)
214
+ res = Net::HTTPResponse.read_new(buf)
215
+ res.reading_body(buf, true) {}
216
+ res
217
+ end
218
+ end
219
+ end
220
+ end
221
+
222
+ module DocumentServer
223
+ class ClientTest < ::Test::Unit::TestCase
224
+ extend T::Sig
225
+ include Test::DocumentServer::Client
226
+
227
+ sig {returns(String)}
228
+ def hi_s
229
+ '{"v":"hi"}'
230
+ end
231
+
232
+ sig {returns(T::Hash[String, T.untyped])}
233
+ def hi_h
234
+ {"v" => "hi"}
235
+ end
236
+
237
+ sig {returns(String)}
238
+ def bye_s
239
+ '{"v":"bye"}'
240
+ end
241
+
242
+ sig {returns(T::Hash[String, T.untyped])}
243
+ def bye_h
244
+ {"v" => "bye"}
245
+ end
246
+
247
+ sig {params(m: String, u: URI::HTTP, req: Net::HTTPRequest).void}
248
+ def check_ruby_request_headers(m, u, req)
249
+ h = {
250
+ "accept" => ["*/*"],
251
+ "accept-encoding" => ["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
252
+ "host" => ["#{u.host}:#{u.port}"],
253
+ "user-agent" => ["Ruby"],
254
+ }
255
+
256
+ if RUBY_VERSION < "3.0.0" && m == "HEAD"
257
+ h.delete("accept-encoding")
258
+ end
259
+
260
+ assert_equal(h, req.to_hash)
261
+ end
262
+
263
+ sig {params(m: String, u: URI::HTTP, w: JwtDecoding, b: T.untyped, req: Net::HTTPRequest).void}
264
+ def check_ruby_request_headers_with_jwt(m, u, w, b, req)
265
+ x = {
266
+ "accept" => ["*/*"],
267
+ "accept-encoding" => ["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
268
+ "host" => ["#{u.host}:#{u.port}"],
269
+ "user-agent" => ["Ruby"],
270
+ }
271
+
272
+ if RUBY_VERSION < "3.0.0" && m == "HEAD"
273
+ x.delete("accept-encoding")
274
+ end
275
+
276
+ y = req.to_hash
277
+
278
+ r = y["authorization"]
279
+ assert_equal(1, r.length)
280
+ assert_true(r[0].start_with?("Bearer "))
281
+
282
+ y.delete("authorization")
283
+ assert_equal(x, y)
284
+
285
+ t = r[0].sub("Bearer ", "")
286
+ assert_equal(b, w.decode_header(t))
287
+ end
288
+
289
+ sig {params(m: String, u: URI::HTTP, w: JwtDecoding, s: String, b: T::Hash[String, T.untyped], req: Net::HTTPRequest).void}
290
+ def check_ruby_request_headers_with_custom_jwt_header(m, u, w, s, b, req)
291
+ x = {
292
+ "accept" => ["*/*"],
293
+ "accept-encoding" => ["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
294
+ "host" => ["#{u.host}:#{u.port}"],
295
+ "user-agent" => ["Ruby"],
296
+ }
297
+
298
+ if RUBY_VERSION < "3.0.0" && m == "HEAD"
299
+ x.delete("accept-encoding")
300
+ end
301
+
302
+ y = req.to_hash
303
+
304
+ r = y[s.downcase]
305
+ assert_equal(1, r.length)
306
+ assert_true(r[0].start_with?("Bearer "))
307
+
308
+ y.delete(s.downcase)
309
+ assert_equal(x, y)
310
+
311
+ t = r[0].sub("Bearer ", "")
312
+ assert_equal(b, w.decode_header(t))
313
+ end
314
+
315
+ sig {params(m: String, u: URI::HTTP, w: JwtDecoding, s: String, b: T::Hash[String, T.untyped], req: Net::HTTPRequest).void}
316
+ def check_ruby_request_headers_with_custom_jwt_schema(m, u, w, s, b, req)
317
+ x = {
318
+ "accept" => ["*/*"],
319
+ "accept-encoding" => ["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
320
+ "host" => ["#{u.host}:#{u.port}"],
321
+ "user-agent" => ["Ruby"],
322
+ }
323
+
324
+ if RUBY_VERSION < "3.0.0" && m == "HEAD"
325
+ x.delete("accept-encoding")
326
+ end
327
+
328
+ y = req.to_hash
329
+
330
+ r = y["authorization"]
331
+ assert_equal(1, r.length)
332
+ assert_true(r[0].start_with?("#{s} "))
333
+
334
+ y.delete("authorization")
335
+ assert_equal(x, y)
336
+
337
+ t = r[0].sub("#{s} ", "")
338
+ assert_equal(b, w.decode_header(t))
339
+ end
340
+
341
+ sig {params(w: JwtDecoding, b: T::Hash[String, T.untyped], body: T.untyped).void}
342
+ def check_body_with_jwt(w, b, body)
343
+ h = T.let(JSON.parse(body), T::Hash[String, T.untyped])
344
+ assert_equal(b, w.decode_body(h))
345
+ end
346
+
347
+ def test_initialize_initializes_with_a_http_and_a_base_uri
348
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
349
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
350
+ c = Client.new(base_uri: u, http: h)
351
+
352
+ assert_not_equal(h.object_id, c.http.object_id)
353
+ assert_equal(h.address, c.http.address)
354
+ assert_equal(h.port, c.http.port)
355
+
356
+ assert_not_equal(u.object_id, c.base_uri.object_id)
357
+ assert_equal(u, c.base_uri)
358
+
359
+ assert_equal(Client::USER_AGENT, c.user_agent)
360
+
361
+ assert_kind_of(Client::CommandService, c.command)
362
+ assert_kind_of(Client::ConversionService, c.conversion)
363
+ assert_kind_of(Client::HealthcheckService, c.healthcheck)
364
+ end
365
+
366
+ def test_initialize_initializes_with_a_http
367
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
368
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
369
+ c = Client.new(http: h)
370
+
371
+ assert_not_equal(h.object_id, c.http.object_id)
372
+ assert_equal(h.address, c.http.address)
373
+ assert_equal(h.port, c.http.port)
374
+
375
+ assert_not_equal(u.object_id, c.base_uri.object_id)
376
+ assert_equal(u, c.base_uri)
377
+
378
+ assert_equal(Client::USER_AGENT, c.user_agent)
379
+
380
+ assert_kind_of(Client::CommandService, c.command)
381
+ assert_kind_of(Client::ConversionService, c.conversion)
382
+ assert_kind_of(Client::HealthcheckService, c.healthcheck)
383
+ end
384
+
385
+ def test_initialize_initializes_with_a_base_uri
386
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
387
+ c = Client.new(base_uri: u)
388
+
389
+ assert_equal(u.hostname, c.http.address)
390
+ assert_equal(u.port, c.http.port)
391
+
392
+ assert_not_equal(u.object_id, c.base_uri.object_id)
393
+ assert_equal(u, c.base_uri)
394
+
395
+ assert_equal(Client::USER_AGENT, c.user_agent)
396
+
397
+ assert_kind_of(Client::CommandService, c.command)
398
+ assert_kind_of(Client::ConversionService, c.conversion)
399
+ assert_kind_of(Client::HealthcheckService, c.healthcheck)
400
+ end
401
+
402
+ def test_initialize_initializes_with_a_user_agent
403
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
404
+ c = Client.new(base_uri: u, user_agent: "my-agent")
405
+
406
+ assert_equal(u.hostname, c.http.address)
407
+ assert_equal(u.port, c.http.port)
408
+
409
+ assert_not_equal(u.object_id, c.base_uri.object_id)
410
+ assert_equal(u, c.base_uri)
411
+
412
+ assert_equal("my-agent", c.user_agent)
413
+
414
+ assert_kind_of(Client::CommandService, c.command)
415
+ assert_kind_of(Client::ConversionService, c.conversion)
416
+ assert_kind_of(Client::HealthcheckService, c.healthcheck)
417
+ end
418
+
419
+ def test_initialize_raises_an_error_if_neither_a_base_uri_nor_a_http_are_provided
420
+ assert_raise_with_message(ArgumentError, "Either http or base_uri must be provided") do
421
+ Client.new
422
+ end
423
+ end
424
+
425
+ def test_with_jwt_creates_a_copy_of_the_client_with_a_jwt
426
+ w = Jwt.new(secret: "***")
427
+ j = Client::Jwt.new(jwt: w)
428
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
429
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
430
+ o = Client.new(base_uri: u, http: h)
431
+
432
+ n = o.with_jwt(j)
433
+ assert_not_equal(o.object_id, n.object_id)
434
+ end
435
+
436
+ def test_with_jwt_preserves_the_request_stack
437
+ for m, n in http_methods
438
+ t = self
439
+
440
+ w = Jwt.new(secret: "***")
441
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
442
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
443
+
444
+ f = T.let(0, Integer)
445
+
446
+ h.define_singleton_method(:request) do |req, body = nil, &block|
447
+ f += 1
448
+ block.call
449
+ t.create_ok("{}")
450
+ end
451
+
452
+ r = h.method(:request)
453
+ h = h.clone
454
+
455
+ h.define_singleton_method(:request) do |req, body = nil, &block|
456
+ f += 1
457
+ r.call(req, body, &block)
458
+ end
459
+
460
+ j = Client::Jwt.new(jwt: w)
461
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
462
+
463
+ res = c.http.request(n.new(u)) do
464
+ f += 1
465
+ end
466
+
467
+ assert_kind_of(Net::HTTPOK, res)
468
+
469
+ assert_equal(3, f)
470
+ assert_equal("{}", res.body)
471
+ end
472
+ end
473
+
474
+ def test_with_jwt_passes_through_itself_when_both_the_request_body_and_the_argument_body_are_present
475
+ for m, n in http_methods
476
+ w = Jwt.new(secret: "***")
477
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
478
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
479
+
480
+ h.define_singleton_method(:start) do
481
+ # Mock the start method to prevent the connection from being
482
+ # established.
483
+ end
484
+
485
+ j = Client::Jwt.new(jwt: w)
486
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
487
+
488
+ r = n.new(u)
489
+ r.body = "{}"
490
+
491
+ assert_raise_with_message(ArgumentError, "both of body argument and HTTPRequest#body set") do
492
+ c.http.request(r, "{}")
493
+ end
494
+ end
495
+ end
496
+
497
+ def test_with_jwt_passes_through_itself_when_both_the_request_body_stream_and_the_argument_body_are_present
498
+ for m, n in http_methods
499
+ w = Jwt.new(secret: "***")
500
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
501
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
502
+
503
+ h.define_singleton_method(:start) do
504
+ # Mock the start method to prevent the connection from being
505
+ # established.
506
+ end
507
+
508
+ j = Client::Jwt.new(jwt: w)
509
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
510
+
511
+ r = n.new(u)
512
+ r.body_stream = StringIO.new("{}")
513
+
514
+ assert_raise_with_message(ArgumentError, "both of body argument and HTTPRequest#body set") do
515
+ c.http.request(r, "{}")
516
+ end
517
+ end
518
+ end
519
+
520
+ def test_with_jwt_injects_the_jwt_into_the_header_when_the_request_body_is_present
521
+ for m, n in http_methods
522
+ t = self
523
+
524
+ w = Jwt.new(secret: "***")
525
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
526
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
527
+
528
+ h.define_singleton_method(:request) do |req, body = nil, &block|
529
+ t.check_request_basics(m, u, req)
530
+ t.check_ruby_request_headers_with_jwt(m, u, w, t.hi_h, req)
531
+ t.assert_equal(t.hi_s, req.body)
532
+ t.assert_nil(body)
533
+ t.assert_nil(block)
534
+ t.create_ok(t.bye_s)
535
+ end
536
+
537
+ j = Client::Jwt.new(jwt: w, locations: [Client::Jwt::Location::Header])
538
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
539
+
540
+ req = n.new(u)
541
+ req.body = t.hi_s
542
+
543
+ res = c.http.request(req)
544
+ assert_equal(t.bye_s, res.body)
545
+ end
546
+ end
547
+
548
+ def test_with_jwt_injects_the_jwt_into_the_body_when_the_request_body_is_present
549
+ for m, n in http_methods
550
+ t = self
551
+
552
+ w = Jwt.new(secret: "***")
553
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
554
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
555
+
556
+ h.define_singleton_method(:request) do |req, body = nil, &block|
557
+ t.check_request_basics(m, u, req)
558
+ t.check_ruby_request_headers(m, u, req)
559
+ t.check_request_body_with_jwt(w, t.hi_h, req)
560
+ t.assert_nil(body)
561
+ t.assert_nil(block)
562
+ t.create_ok(t.bye_s)
563
+ end
564
+
565
+ j = Client::Jwt.new(jwt: w, locations: [Client::Jwt::Location::Body])
566
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
567
+
568
+ req = n.new(u)
569
+ req.body = t.hi_s
570
+
571
+ res = c.http.request(req)
572
+ assert_equal(t.bye_s, res.body)
573
+ end
574
+ end
575
+
576
+ def test_with_jwt_injects_the_jwt_into_multiple_locations_when_the_request_body_is_present
577
+ for m, n in http_methods
578
+ t = self
579
+
580
+ w = Jwt.new(secret: "***")
581
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
582
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
583
+
584
+ h.define_singleton_method(:request) do |req, body = nil, &block|
585
+ t.check_request_basics(m, u, req)
586
+ t.check_ruby_request_headers_with_jwt(m, u, w, t.hi_h, req)
587
+ t.check_request_body_with_jwt(w, t.hi_h, req)
588
+ t.assert_nil(body)
589
+ t.assert_nil(block)
590
+ t.create_ok(t.bye_s)
591
+ end
592
+
593
+ j = Client::Jwt.new(
594
+ jwt: w,
595
+ locations: [
596
+ Client::Jwt::Location::Header,
597
+ Client::Jwt::Location::Body,
598
+ ],
599
+ )
600
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
601
+
602
+ req = n.new(u)
603
+ req.body = t.hi_s
604
+
605
+ res = c.http.request(req)
606
+ assert_equal(t.bye_s, res.body)
607
+ end
608
+ end
609
+
610
+ def test_with_jwt_does_not_inject_the_jwt_without_a_location_when_the_request_body_is_present
611
+ for m, n in http_methods
612
+ t = self
613
+
614
+ w = Jwt.new(secret: "***")
615
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
616
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
617
+
618
+ h.define_singleton_method(:request) do |req, body = nil, &block|
619
+ t.check_request_basics(m, u, req)
620
+ t.check_ruby_request_headers(m, u, req)
621
+ t.assert_equal(t.hi_s, req.body)
622
+ t.assert_nil(body)
623
+ t.assert_nil(block)
624
+ t.create_ok(t.bye_s)
625
+ end
626
+
627
+ j = Client::Jwt.new(jwt: w, locations: [])
628
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
629
+
630
+ req = n.new(u)
631
+ req.body = t.hi_s
632
+
633
+ res = c.http.request(req)
634
+ assert_equal(t.bye_s, res.body)
635
+ end
636
+ end
637
+
638
+ def test_with_jwt_injects_the_jwt_with_custom_header_when_the_request_body_is_present
639
+ for m, n in http_methods
640
+ t = self
641
+
642
+ w = Jwt.new(secret: "***")
643
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
644
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
645
+
646
+ h.define_singleton_method(:request) do |req, body = nil, &block|
647
+ t.check_request_basics(m, u, req)
648
+ t.check_ruby_request_headers_with_custom_jwt_header(m, u, w, "X-Auth", t.hi_h, req)
649
+ t.check_request_body_with_jwt(w, t.hi_h, req)
650
+ t.assert_nil(body)
651
+ t.assert_nil(block)
652
+ t.create_ok(t.bye_s)
653
+ end
654
+
655
+ j = Client::Jwt.new(jwt: w, header: "X-Auth")
656
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
657
+
658
+ req = n.new(u)
659
+ req.body = t.hi_s
660
+
661
+ res = c.http.request(req)
662
+ assert_equal(t.bye_s, res.body)
663
+ end
664
+ end
665
+
666
+ def test_with_jwt_injects_the_jwt_with_custom_schema_when_the_request_body_is_present
667
+ for m, n in http_methods
668
+ t = self
669
+
670
+ w = Jwt.new(secret: "***")
671
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
672
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
673
+
674
+ h.define_singleton_method(:request) do |req, body = nil, &block|
675
+ t.check_request_basics(m, u, req)
676
+ t.check_ruby_request_headers_with_custom_jwt_schema(m, u, w, "Token", t.hi_h, req)
677
+ t.check_request_body_with_jwt(w, t.hi_h, req)
678
+ t.assert_nil(body)
679
+ t.assert_nil(block)
680
+ t.create_ok(t.bye_s)
681
+ end
682
+
683
+ j = Client::Jwt.new(jwt: w, schema: "Token")
684
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
685
+
686
+ req = n.new(u)
687
+ req.body = t.hi_s
688
+
689
+ res = c.http.request(req)
690
+ assert_equal(t.bye_s, res.body)
691
+ end
692
+ end
693
+
694
+ def test_with_jwt_does_not_inject_the_jwt_when_the_request_body_stream_is_present
695
+ for m, n in http_methods
696
+ t = self
697
+
698
+ w = Jwt.new(secret: "***")
699
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
700
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
701
+
702
+ h.define_singleton_method(:request) do |req, body = nil, &block|
703
+ t.check_request_basics(m, u, req)
704
+ t.check_ruby_request_headers(m, u, req)
705
+ t.assert_nil(req.body)
706
+ t.assert_nil(body)
707
+ t.assert_nil(block)
708
+ t.create_ok(t.bye_s)
709
+ end
710
+
711
+ j = Client::Jwt.new(jwt: w)
712
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
713
+
714
+ req = n.new(u)
715
+ req.body_stream = StringIO.new(t.hi_s)
716
+
717
+ res = c.http.request(req)
718
+ assert_equal(t.bye_s, res.body)
719
+ end
720
+ end
721
+
722
+ def test_with_jwt_injects_the_jwt_into_the_header_when_the_argument_body_is_present
723
+ for m, n in http_methods
724
+ t = self
725
+
726
+ w = Jwt.new(secret: "***")
727
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
728
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
729
+
730
+ h.define_singleton_method(:request) do |req, body = nil, &block|
731
+ t.check_request_basics(m, u, req)
732
+ t.check_ruby_request_headers_with_jwt(m, u, w, t.hi_h, req)
733
+ t.assert_nil(req.body)
734
+ t.assert_equal(t.hi_s, body)
735
+ t.assert_nil(block)
736
+ t.create_ok(t.bye_s)
737
+ end
738
+
739
+ j = Client::Jwt.new(jwt: w, locations: [Client::Jwt::Location::Header])
740
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
741
+
742
+ res = c.http.request(n.new(u), t.hi_s)
743
+ assert_equal(t.bye_s, res.body)
744
+ end
745
+ end
746
+
747
+ def test_with_jwt_injects_the_jwt_into_the_body_when_the_argument_body_is_present
748
+ for m, n in http_methods
749
+ t = self
750
+
751
+ w = Jwt.new(secret: "***")
752
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
753
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
754
+
755
+ h.define_singleton_method(:request) do |req, body = nil, &block|
756
+ t.check_request_basics(m, u, req)
757
+ t.check_ruby_request_headers(m, u, req)
758
+ t.assert_nil(req.body)
759
+ t.check_body_with_jwt(w, t.hi_h, body)
760
+ t.assert_nil(block)
761
+ t.create_ok(t.bye_s)
762
+ end
763
+
764
+ j = Client::Jwt.new(jwt: w, locations: [Client::Jwt::Location::Body])
765
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
766
+
767
+ res = c.http.request(n.new(u), t.hi_s)
768
+ assert_equal(t.bye_s, res.body)
769
+ end
770
+ end
771
+
772
+ def test_with_jwt_injects_the_jwt_into_multiple_locations_when_the_argument_body_is_present
773
+ for m, n in http_methods
774
+ t = self
775
+
776
+ w = Jwt.new(secret: "***")
777
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
778
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
779
+
780
+ h.define_singleton_method(:request) do |req, body = nil, &block|
781
+ t.check_request_basics(m, u, req)
782
+ t.check_ruby_request_headers_with_jwt(m, u, w, t.hi_h, req)
783
+ t.assert_nil(req.body)
784
+ t.check_body_with_jwt(w, t.hi_h, body)
785
+ t.assert_nil(block)
786
+ t.create_ok(t.bye_s)
787
+ end
788
+
789
+ j = Client::Jwt.new(
790
+ jwt: w,
791
+ locations: [
792
+ Client::Jwt::Location::Header,
793
+ Client::Jwt::Location::Body,
794
+ ],
795
+ )
796
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
797
+
798
+ res = c.http.request(n.new(u), t.hi_s)
799
+ assert_equal(t.bye_s, res.body)
800
+ end
801
+ end
802
+
803
+ def test_with_jwt_does_not_inject_the_jwt_without_a_location_when_the_argument_body_is_present
804
+ for m, n in http_methods
805
+ t = self
806
+
807
+ w = Jwt.new(secret: "***")
808
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
809
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
810
+
811
+ h.define_singleton_method(:request) do |req, body = nil, &block|
812
+ t.check_request_basics(m, u, req)
813
+ t.check_ruby_request_headers(m, u, req)
814
+ t.assert_nil(req.body)
815
+ t.assert_equal(t.hi_s, body)
816
+ t.assert_nil(block)
817
+ t.create_ok(t.bye_s)
818
+ end
819
+
820
+ j = Client::Jwt.new(jwt: w, locations: [])
821
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
822
+
823
+ res = c.http.request(n.new(u), t.hi_s)
824
+ assert_equal(t.bye_s, res.body)
825
+ end
826
+ end
827
+
828
+ def test_with_jwt_injects_the_jwt_with_custom_header_when_the_argument_body_is_present
829
+ for m, n in http_methods
830
+ t = self
831
+
832
+ w = Jwt.new(secret: "***")
833
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
834
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
835
+
836
+ h.define_singleton_method(:request) do |req, body = nil, &block|
837
+ t.check_request_basics(m, u, req)
838
+ t.check_ruby_request_headers_with_custom_jwt_header(m, u, w, "X-Auth", t.hi_h, req)
839
+ t.assert_nil(req.body)
840
+ t.check_body_with_jwt(w, t.hi_h, body)
841
+ t.assert_nil(block)
842
+ t.create_ok(t.bye_s)
843
+ end
844
+
845
+ j = Client::Jwt.new(jwt: w, header: "X-Auth")
846
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
847
+
848
+ res = c.http.request(n.new(u), t.hi_s)
849
+ assert_equal(t.bye_s, res.body)
850
+ end
851
+ end
852
+
853
+ def test_with_jwt_injects_the_jwt_with_custom_schema_when_the_argument_body_is_present
854
+ for m, n in http_methods
855
+ t = self
856
+
857
+ w = Jwt.new(secret: "***")
858
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
859
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
860
+
861
+ h.define_singleton_method(:request) do |req, body = nil, &block|
862
+ t.check_request_basics(m, u, req)
863
+ t.check_ruby_request_headers_with_custom_jwt_schema(m, u, w, "Token", t.hi_h, req)
864
+ t.assert_nil(req.body)
865
+ t.check_body_with_jwt(w, t.hi_h, body)
866
+ t.assert_nil(block)
867
+ t.create_ok(t.bye_s)
868
+ end
869
+
870
+ j = Client::Jwt.new(jwt: w, schema: "Token")
871
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
872
+
873
+ res = c.http.request(n.new(u), t.hi_s)
874
+ assert_equal(t.bye_s, res.body)
875
+ end
876
+ end
877
+
878
+ def test_uri_creates_a_uri
879
+ cases = [
880
+ ["http://localhost:8080", "", "http://localhost:8080"],
881
+ ["http://localhost:8080", "/", "http://localhost:8080/"],
882
+ ["http://localhost:8080", "b", "http://localhost:8080/b"],
883
+ ["http://localhost:8080", "/b", "http://localhost:8080/b"],
884
+ ["http://localhost:8080", "b/", "http://localhost:8080/b/"],
885
+ ["http://localhost:8080", "/b/", "http://localhost:8080/b/"],
886
+
887
+ ["http://localhost:8080/", "", "http://localhost:8080/"],
888
+ ["http://localhost:8080/", "/", "http://localhost:8080/"],
889
+ ["http://localhost:8080/", "b", "http://localhost:8080/b"],
890
+ ["http://localhost:8080/", "/b", "http://localhost:8080/b"],
891
+ ["http://localhost:8080/", "b/", "http://localhost:8080/b/"],
892
+ ["http://localhost:8080/", "/b/", "http://localhost:8080/b/"],
893
+
894
+ ["http://localhost:8080/a", "", "http://localhost:8080/a"],
895
+ ["http://localhost:8080/a", "/", "http://localhost:8080/"],
896
+ ["http://localhost:8080/a", "b", "http://localhost:8080/b"],
897
+ ["http://localhost:8080/a", "/b", "http://localhost:8080/b"],
898
+ ["http://localhost:8080/a", "b/", "http://localhost:8080/b/"],
899
+ ["http://localhost:8080/a", "/b/", "http://localhost:8080/b/"],
900
+
901
+ ["http://localhost:8080/a/", "", "http://localhost:8080/a/"],
902
+ ["http://localhost:8080/a/", "/", "http://localhost:8080/"],
903
+ ["http://localhost:8080/a/", "b", "http://localhost:8080/a/b"],
904
+ ["http://localhost:8080/a/", "/b", "http://localhost:8080/b"],
905
+ ["http://localhost:8080/a/", "b/", "http://localhost:8080/a/b/"],
906
+ ["http://localhost:8080/a/", "/b/", "http://localhost:8080/b/"],
907
+ ]
908
+
909
+ for f, p, t in cases
910
+ u = T.cast(URI.parse(f), URI::HTTP)
911
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
912
+ c = Client.new(base_uri: u, http: h)
913
+
914
+ u = c.uri(p)
915
+ assert_equal(t, u.to_s)
916
+ end
917
+ end
918
+
919
+ def test_request_creates_a_request
920
+ for m, n in http_methods
921
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
922
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
923
+ c = Client.new(base_uri: u, http: h)
924
+
925
+ r = c.request(n, u)
926
+ check_request_basics(m, u, r)
927
+ check_request_headers(m, u, r)
928
+ assert_nil(r.body)
929
+ end
930
+ end
931
+
932
+ def test_request_creates_a_request_with_a_user_agent
933
+ for m, n in http_methods
934
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
935
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
936
+ c = Client.new(base_uri: u, http: h, user_agent: "my-agent")
937
+
938
+ r = c.request(n, u)
939
+ check_request_basics(m, u, r)
940
+ check_request_headers_with_custom_user_agent(m, u, "my-agent", r)
941
+ assert_nil(r.body)
942
+ end
943
+ end
944
+
945
+ def test_request_creates_a_request_with_a_body
946
+ for m, n in http_methods
947
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
948
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
949
+ c = Client.new(base_uri: u, http: h)
950
+
951
+ r = c.request(n, u, hi_h)
952
+ check_request_basics(m, u, r)
953
+ check_request_headers_with_content_type(m, u, r)
954
+ assert_equal(hi_s, r.body)
955
+ end
956
+ end
957
+
958
+ def test_do_does_the_request
959
+ for m, n in http_methods
960
+ t = self
961
+
962
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
963
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
964
+
965
+ h.define_singleton_method(:request) do |req, body = nil, &block|
966
+ t.check_request_basics(m, u, req)
967
+ t.check_request_headers(m, u, req)
968
+ t.assert_nil(req.body)
969
+ t.assert_nil(body)
970
+ t.assert_nil(block)
971
+ t.create_ok(t.bye_s)
972
+ end
973
+
974
+ c = Client.new(base_uri: u, http: h)
975
+
976
+ req = c.request(n, u)
977
+ b, res = c.do(req)
978
+ assert_nil(res.error)
979
+
980
+ assert_equal(req, res.request)
981
+ assert_equal(bye_s, res.response.body)
982
+ assert_equal(bye_h, b)
983
+ end
984
+ end
985
+
986
+ def test_do_does_the_request_with_the_user_agent
987
+ for m, n in http_methods
988
+ t = self
989
+
990
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
991
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
992
+
993
+ h.define_singleton_method(:request) do |req, body = nil, &block|
994
+ t.check_request_basics(m, u, req)
995
+ t.check_request_headers_with_custom_user_agent(m, u, "my-agent", req)
996
+ t.assert_nil(req.body)
997
+ t.assert_nil(body)
998
+ t.assert_nil(block)
999
+ t.create_ok(t.bye_s)
1000
+ end
1001
+
1002
+ c = Client.new(base_uri: u, http: h, user_agent: "my-agent")
1003
+
1004
+ req = c.request(n, u)
1005
+ b, res = c.do(req)
1006
+ assert_nil(res.error)
1007
+
1008
+ assert_equal(req, res.request)
1009
+ assert_equal(bye_s, res.response.body)
1010
+ assert_equal(bye_h, b)
1011
+ end
1012
+ end
1013
+
1014
+ def test_do_does_the_request_with_the_body
1015
+ for m, n in http_methods
1016
+ t = self
1017
+
1018
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
1019
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
1020
+
1021
+ h.define_singleton_method(:request) do |req, body = nil, &block|
1022
+ t.check_request_basics(m, u, req)
1023
+ t.check_request_headers_with_content_type(m, u, req)
1024
+ t.assert_equal(t.hi_s, req.body)
1025
+ t.assert_nil(body)
1026
+ t.assert_nil(block)
1027
+ t.create_ok(t.bye_s)
1028
+ end
1029
+
1030
+ c = Client.new(base_uri: u, http: h)
1031
+
1032
+ req = c.request(n, u, hi_h)
1033
+ b, res = c.do(req)
1034
+ assert_nil(res.error)
1035
+
1036
+ assert_equal(req, res.request)
1037
+ assert_equal(bye_s, res.response.body)
1038
+ assert_equal(bye_h, b)
1039
+ end
1040
+ end
1041
+
1042
+ def test_do_returns_an_error_if_the_response_body_is_invalid_json
1043
+ for m, n in http_methods
1044
+ t = self
1045
+
1046
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
1047
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
1048
+
1049
+ h.define_singleton_method(:request) do |req, body = nil, &block|
1050
+ t.check_request_basics(m, u, req)
1051
+ t.check_request_headers(m, u, req)
1052
+ t.assert_nil(req.body)
1053
+ t.assert_nil(body)
1054
+ t.assert_nil(block)
1055
+ t.create_ok("}")
1056
+ end
1057
+
1058
+ c = Client.new(base_uri: u, http: h)
1059
+
1060
+ req = c.request(n, u)
1061
+ b, res = c.do(req)
1062
+
1063
+ err = T.cast(res.error, JSON::ParserError)
1064
+ assert_equal("unexpected token at '}'", err.message)
1065
+
1066
+ assert_equal(req, res.request)
1067
+ assert_equal("}", res.response.body)
1068
+ assert_nil(b)
1069
+ end
1070
+ end
1071
+
1072
+ def test_do_does_the_request_with_the_jwt_in_the_header_location
1073
+ for m, n in http_methods
1074
+ t = self
1075
+
1076
+ w = Jwt.new(secret: "***")
1077
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
1078
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
1079
+
1080
+ h.define_singleton_method(:request) do |req, body = nil, &block|
1081
+ t.check_request_basics(m, u, req)
1082
+ t.check_request_headers_with_jwt(m, u, w, t.hi_h, req)
1083
+ t.assert_equal(t.hi_s, req.body)
1084
+ t.assert_nil(body)
1085
+ t.assert_nil(block)
1086
+ t.create_ok(t.bye_s)
1087
+ end
1088
+
1089
+ j = Client::Jwt.new(jwt: w, locations: [Client::Jwt::Location::Header])
1090
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
1091
+
1092
+ req = c.request(n, u, hi_h)
1093
+ b, res = c.do(req)
1094
+ assert_nil(res.error)
1095
+
1096
+ assert_equal(req, res.request)
1097
+ assert_equal(bye_s, res.response.body)
1098
+ assert_equal(bye_h, b)
1099
+ end
1100
+ end
1101
+
1102
+ def test_do_does_the_request_with_the_jwt_in_the_body_location
1103
+ for m, n in http_methods
1104
+ t = self
1105
+
1106
+ w = Jwt.new(secret: "***")
1107
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
1108
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
1109
+
1110
+ h.define_singleton_method(:request) do |req, body = nil, &block|
1111
+ t.check_request_basics(m, u, req)
1112
+ t.check_request_headers_with_content_type(m, u, req)
1113
+ t.check_request_body_with_jwt(w, t.hi_h, req)
1114
+ t.assert_nil(body)
1115
+ t.assert_nil(block)
1116
+ t.create_ok(t.bye_s)
1117
+ end
1118
+
1119
+ j = Client::Jwt.new(jwt: w, locations: [Client::Jwt::Location::Body])
1120
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
1121
+
1122
+ req = c.request(n, u, hi_h)
1123
+ b, res = c.do(req)
1124
+ assert_nil(res.error)
1125
+
1126
+ assert_equal(req, res.request)
1127
+ assert_equal(bye_s, res.response.body)
1128
+ assert_equal(bye_h, b)
1129
+ end
1130
+ end
1131
+
1132
+ def test_do_does_the_request_with_the_jwt_in_multiple_locations
1133
+ for m, n in http_methods
1134
+ t = self
1135
+
1136
+ w = Jwt.new(secret: "***")
1137
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
1138
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
1139
+
1140
+ h.define_singleton_method(:request) do |req, body = nil, &block|
1141
+ t.check_request_basics(m, u, req)
1142
+ t.check_request_headers_with_jwt(m, u, w, t.hi_h, req)
1143
+ t.check_request_body_with_jwt(w, t.hi_h, req)
1144
+ t.assert_nil(body)
1145
+ t.assert_nil(block)
1146
+ t.create_ok(t.bye_s)
1147
+ end
1148
+
1149
+ j = Client::Jwt.new(
1150
+ jwt: w,
1151
+ locations: [
1152
+ Client::Jwt::Location::Header,
1153
+ Client::Jwt::Location::Body,
1154
+ ],
1155
+ )
1156
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
1157
+
1158
+ req = c.request(n, u, hi_h)
1159
+ b, res = c.do(req)
1160
+ assert_nil(res.error)
1161
+
1162
+ assert_equal(req, res.request)
1163
+ assert_equal(bye_s, res.response.body)
1164
+ assert_equal(bye_h, b)
1165
+ end
1166
+ end
1167
+
1168
+ def test_do_does_the_request_without_the_jwt_when_no_location_is_specified
1169
+ for m, n in http_methods
1170
+ t = self
1171
+
1172
+ w = Jwt.new(secret: "***")
1173
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
1174
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
1175
+
1176
+ h.define_singleton_method(:request) do |req, body = nil, &block|
1177
+ t.check_request_basics(m, u, req)
1178
+ t.check_request_headers_with_content_type(m, u, req)
1179
+ t.assert_equal(t.hi_s, req.body)
1180
+ t.assert_nil(body)
1181
+ t.assert_nil(block)
1182
+ t.create_ok(t.bye_s)
1183
+ end
1184
+
1185
+ j = Client::Jwt.new(jwt: w, locations: [])
1186
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
1187
+
1188
+ req = c.request(n, u, hi_h)
1189
+ b, res = c.do(req)
1190
+ assert_nil(res.error)
1191
+
1192
+ assert_equal(req, res.request)
1193
+ assert_equal(bye_s, res.response.body)
1194
+ assert_equal(bye_h, b)
1195
+ end
1196
+ end
1197
+
1198
+ def test_do_does_the_request_with_custom_jwt_header
1199
+ for m, n in http_methods
1200
+ t = self
1201
+ w = Jwt.new(secret: "***")
1202
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
1203
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
1204
+
1205
+ h.define_singleton_method(:request) do |req, body = nil, &block|
1206
+ t.check_request_basics(m, u, req)
1207
+ t.check_request_headers_with_custom_jwt_header(m, u, w, "X-Auth", t.hi_h, req)
1208
+ t.check_request_body_with_jwt(w, t.hi_h, req)
1209
+ t.assert_nil(body)
1210
+ t.assert_nil(block)
1211
+ t.create_ok(t.bye_s)
1212
+ end
1213
+
1214
+ j = Client::Jwt.new(jwt: w, header: "X-Auth")
1215
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
1216
+
1217
+ req = c.request(n, u, t.hi_h)
1218
+ b, res = c.do(req)
1219
+ assert_nil(res.error)
1220
+
1221
+ assert_equal(req, res.request)
1222
+ assert_equal(bye_s, res.response.body)
1223
+ assert_equal(bye_h, b)
1224
+ end
1225
+ end
1226
+
1227
+ def test_do_does_the_request_with_custom_jwt_schema
1228
+ for m, n in http_methods
1229
+ t = self
1230
+
1231
+ w = Jwt.new(secret: "***")
1232
+ u = T.cast(URI.parse("http://localhost:8080/"), URI::HTTP)
1233
+ h = T.let(Net::HTTP.new(u.hostname, u.port), Net::HTTP)
1234
+
1235
+ h.define_singleton_method(:request) do |req, body = nil, &block|
1236
+ t.check_request_basics(m, u, req)
1237
+ t.check_request_headers_with_custom_jwt_schema(m, u, w, "Token", t.hi_h, req)
1238
+ t.check_request_body_with_jwt(w, t.hi_h, req)
1239
+ t.assert_nil(body)
1240
+ t.assert_nil(block)
1241
+ t.create_ok(t.bye_s)
1242
+ end
1243
+
1244
+ j = Client::Jwt.new(jwt: w, schema: "Token")
1245
+ c = Client.new(base_uri: u, http: h).with_jwt(j)
1246
+
1247
+ req = c.request(n, u, t.hi_h)
1248
+ b, res = c.do(req)
1249
+ assert_nil(res.error)
1250
+
1251
+ assert_equal(req, res.request)
1252
+ assert_equal(bye_s, res.response.body)
1253
+ assert_equal(bye_h, b)
1254
+ end
1255
+ end
1256
+ end
1257
+ end
1258
+ end
1259
+ end