File: response_spec.rb

package info (click to toggle)
ruby-oauth2 2.0.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,608 kB
  • sloc: ruby: 5,441; javascript: 529; makefile: 4; sh: 4
file content (724 lines) | stat: -rw-r--r-- 29,478 bytes parent folder | download
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
# frozen_string_literal: true

RSpec.describe OAuth2::Response do
  subject { described_class.new(raw_response) }

  let(:raw_response) { Faraday::Response.new(status: status, response_headers: headers, body: body) }
  let(:status) { 200 }
  let(:headers) { {"foo" => "bar"} }
  let(:body) { "foo" }

  describe "#initialize" do
    it "returns the status, headers and body" do
      expect(subject.headers).to eq(headers)
      expect(subject.status).to eq(status)
      expect(subject.body).to eq(body)
    end
  end

  describe ".register_parser" do
    let(:response) do
      double(
        "response",
        headers: {"Content-Type" => "application/foo-bar"},
        status: 200,
        body: "baz",
      )
    end

    before do
      described_class.register_parser(:foobar, ["application/foo-bar"]) do |body|
        "foobar #{body}"
      end
    end

    it "adds to the content types and parsers" do
      expect(described_class.send(:class_variable_get, :@@parsers).keys).to include(:foobar)
      expect(described_class.send(:class_variable_get, :@@content_types).keys).to include("application/foo-bar")
    end

    it "is able to parse that content type automatically" do
      expect(described_class.new(response).parsed).to eq("foobar baz")
    end
  end

  describe "#content_type" do
    context "when headers are blank" do
      let(:headers) { nil }

      it "returns nil" do
        expect(subject.content_type).to be_nil
      end
    end

    context "when content-type is not present" do
      let(:headers) { {"a fuzzy" => "fuzzer"} }

      it "returns empty string" do
        expect(subject.content_type).to eq("")
      end
    end

    context "when content-type is present" do
      let(:headers) { {"Content-Type" => "application/x-www-form-urlencoded"} }

      it "returns the content type header contents" do
        expect(subject.content_type).to eq("application/x-www-form-urlencoded")
      end
    end
  end

  describe "#parsed" do
    subject(:parsed) do
      headers = {"Content-Type" => content_type}
      response = double("response", headers: headers, body: body)
      instance = described_class.new(response)
      instance.parsed
    end

    shared_examples_for "parsing JSON-like" do
      it "has num keys" do
        expect(parsed.keys.size).to eq(6)
      end

      it "parses string" do
        expect(parsed["foo"]).to eq("bar")
        expect(parsed.key("bar")).to eq("foo")
      end

      it "parses non-zero number" do
        expect(parsed["answer"]).to eq(42)
        expect(parsed.key(42)).to eq("answer")
      end

      it "parses nil as NilClass" do
        expect(parsed["krill"]).to be_nil
        expect(parsed.key(nil)).to eq("krill")
      end

      it "parses zero as number" do
        expect(parsed["zero"]).to eq(0)
        expect(parsed.key(0)).to eq("zero")
      end

      it "parses false as FalseClass" do
        expect(parsed["malign"]).to be(false)
        expect(parsed.key(false)).to eq("malign")
      end

      it "parses false as TrueClass" do
        expect(parsed["shine"]).to be(true)
        expect(parsed.key(true)).to eq("shine")
      end
    end

    context "when application/json" do
      let(:content_type) { "application/json" }
      let(:body) { JSON.dump(foo: "bar", answer: 42, krill: nil, zero: 0, malign: false, shine: true) }

      it_behaves_like "parsing JSON-like"
    end

    context "when application/Json" do
      let(:content_type) { "application/Json" }
      let(:body) { JSON.dump(foo: "bar", answer: 42, krill: nil, zero: 0, malign: false, shine: true) }

      it_behaves_like "parsing JSON-like"
    end

    context "when application/hal+json" do
      let(:content_type) { "application/hal+json" }
      let(:body) { JSON.dump(foo: "bar", answer: 42, krill: nil, zero: 0, malign: false, shine: true) }

      it_behaves_like "parsing JSON-like"
    end

    context "when application/x-www-form-urlencoded" do
      let(:content_type) { "application/x-www-form-urlencoded" }
      let(:body) { "foo=bar&answer=42&krill=&zero=0&malign=false&shine=true" }

      it "has num keys" do
        expect(parsed.keys.size).to eq(6)
      end

      it "parses string" do
        expect(parsed["foo"]).to eq("bar")
        expect(parsed.key("bar")).to eq("foo")
      end

      it "parses non-zero number as string" do
        expect(parsed["answer"]).to eq("42")
        expect(parsed.key("42")).to eq("answer")
      end

      it "parses nil as empty string" do
        expect(parsed["krill"]).to eq("")
        expect(parsed.key("")).to eq("krill")
      end

      it "parses zero as string" do
        expect(parsed["zero"]).to eq("0")
        expect(parsed.key("0")).to eq("zero")
      end

      it "parses false as string" do
        expect(parsed["malign"]).to eq("false")
        expect(parsed.key("false")).to eq("malign")
      end

      it "parses true as string" do
        expect(parsed["shine"]).to eq("true")
        expect(parsed.key("true")).to eq("shine")
      end
    end

    it "parses application/vnd.collection+json body" do
      headers = {"Content-Type" => "application/vnd.collection+json"}
      body = JSON.dump(collection: {})
      response = double("response", headers: headers, body: body)
      subject = described_class.new(response)
      expect(subject.parsed.keys.size).to eq(1)
    end

    it "parses application/vnd.api+json body" do
      headers = {"Content-Type" => "application/vnd.api+json"}
      body = JSON.dump(collection: {})
      response = double("response", headers: headers, body: body)
      subject = described_class.new(response)
      expect(subject.parsed.keys.size).to eq(1)
    end

    it "parses application/problem+json body" do
      headers = {"Content-Type" => "application/problem+json"}
      body = JSON.dump(type: "https://tools.ietf.org/html/rfc7231#section-6.5.4", title: "Not Found")
      response = double("response", headers: headers, body: body)
      subject = described_class.new(response)
      expect(subject.parsed.keys.size).to eq(2)
      expect(subject.parsed["type"]).to eq("https://tools.ietf.org/html/rfc7231#section-6.5.4")
      expect(subject.parsed["title"]).to eq("Not Found")
    end

    it "doesn't try to parse other content-types" do
      headers = {"Content-Type" => "text/html"}
      body = "<!DOCTYPE html><html><head></head><body></body></html>"

      response = double("response", headers: headers, body: body)

      expect(JSON).not_to receive(:parse)
      expect(Rack::Utils).not_to receive(:parse_query)

      subject = described_class.new(response)
      expect(subject.parsed).to be_nil
    end

    it "doesn't parse bodies which have previously been parsed" do
      headers = {"Content-Type" => "application/json"}
      body = {foo: "bar", answer: 42, krill: nil, zero: 0, malign: false, shine: true}

      response = double("response", headers: headers, body: body)

      expect(JSON).not_to receive(:parse)
      expect(Rack::Utils).not_to receive(:parse_query)

      subject = described_class.new(response)
      expect(subject.parsed.keys.size).to eq(6)
    end

    it "snakecases json keys when parsing" do
      headers = {"Content-Type" => "application/json"}
      body = JSON.dump("accessToken" => "bar", "MiGever" => "Ani")
      response = double("response", headers: headers, body: body)
      subject = described_class.new(response)
      expect(subject.parsed.keys.size).to eq(2)
      expect(subject.parsed["access_token"]).to eq("bar")
      expect(subject.parsed["mi_gever"]).to eq("Ani")
    end

    context "when not snaky" do
      it "does not snakecase json keys when parsing" do
        headers = {"Content-Type" => "application/json"}
        body = JSON.dump("accessToken" => "bar", "MiGever" => "Ani")
        response = double("response", headers: headers, body: body)
        subject = described_class.new(response, snaky: false)
        expect(subject.parsed.keys.size).to eq(2)
        expect(subject.parsed["accessToken"]).to eq("bar")
        expect(subject.parsed["MiGever"]).to eq("Ani")
        expect(subject.parsed["access_token"]).to be_nil
        expect(subject.parsed["mi_gever"]).to be_nil
      end
    end

    it "supports registered parsers with arity == 0; passing nothing" do
      described_class.register_parser(:arity_0, []) do
        "a-ok"
      end

      headers = {"Content-Type" => "text/html"}
      body = "<!DOCTYPE html><html><head></head><body></body></html>"
      response = double("response", headers: headers, body: body)

      subject = described_class.new(response, parse: :arity_0)

      expect(subject.parsed).to eq("a-ok")
    end

    it "supports registered parsers with arity == 2; passing body and response" do
      headers = {"Content-Type" => "text/html"}
      body = "<!DOCTYPE html><html><head></head><body></body></html>"
      response = double("response", headers: headers, body: body)

      described_class.register_parser(:arity_2, []) do |passed_body, passed_response|
        expect(passed_body).to eq(body)
        expect(passed_response).to eq(response)

        "a-ok"
      end

      subject = described_class.new(response, parse: :arity_2)

      expect(subject.parsed).to eq("a-ok")
    end

    it "supports registered parsers with arity > 2; passing body and response" do
      headers = {"Content-Type" => "text/html"}
      body = "<!DOCTYPE html><html><head></head><body></body></html>"
      response = double("response", headers: headers, body: body)

      described_class.register_parser(:arity_3, []) do |passed_body, passed_response, *args|
        expect(passed_body).to eq(body)
        expect(passed_response).to eq(response)
        expect(args).to eq([])

        "a-ok"
      end

      subject = described_class.new(response, parse: :arity_3)

      expect(subject.parsed).to eq("a-ok")
    end

    it "supports directly passed parsers" do
      headers = {"Content-Type" => "text/html"}
      body = "<!DOCTYPE html><html><head></head><body></body></html>"
      response = double("response", headers: headers, body: body)

      subject = described_class.new(response, parse: -> { "a-ok" })

      expect(subject.parsed).to eq("a-ok")
    end

    it "supports no parsing" do
      headers = {"Content-Type" => "text/html"}
      body = "<!DOCTYPE html><html><head></head><body></body></html>"
      response = double("response", headers: headers, body: body)

      subject = described_class.new(response, parse: false)

      expect(subject.parsed).to be_nil
    end
  end

  context "with xml parser registration" do
    it "tries to load multi_xml.rb and use it" do
      expect(described_class.send(:class_variable_get, :@@parsers)[:xml]).not_to be_nil
    end

    it "is able to parse xml" do
      headers = {"Content-Type" => "text/xml"}
      body = '<?xml version="1.0" standalone="yes" ?><foo><bar>baz</bar></foo>'

      response = double("response", headers: headers, body: body)
      expect(described_class.new(response).parsed).to eq("foo" => {"bar" => "baz"})
    end

    it "is able to parse application/xml" do
      headers = {"Content-Type" => "application/xml"}
      body = '<?xml version="1.0" standalone="yes" ?><foo><bar>baz</bar></foo>'

      response = double("response", headers: headers, body: body)
      expect(described_class.new(response).parsed).to eq("foo" => {"bar" => "baz"})
    end
  end

  describe "converting to json" do
    it "does not blow up" do
      expect { subject.to_json }.not_to raise_error
    end
  end

  describe "with custom vanilla snaky_hash_klass" do
    let(:parsed_response) { {"some_key" => "some_value"} }
    let(:custom_hash_class) do
      Class.new(Hash)
    end

    before do
      @response = double(
        "response",
        headers: {"Content-Type" => "application/json"},
        status: 200,
        body: parsed_response.to_json,
      )
    end

    it "uses the specified hash class when snaky is true" do
      response = described_class.new(@response, parse: :automatic, snaky: true, snaky_hash_klass: custom_hash_class)
      expect(response.parsed).to be_a(custom_hash_class)
      expect(response.parsed).not_to be_a(OAuth2::Response::DEFAULT_OPTIONS[:snaky_hash_klass])
      expect(response.parsed).to eq({"some_key" => "some_value"})
      expect(response.parsed["some_key"]).to eq("some_value")
    end

    it "uses the default hash class when snaky_hash_klass is not specified" do
      response = described_class.new(@response, parse: :automatic, snaky: true)
      expect(response.parsed).not_to be_a(custom_hash_class)
      expect(response.parsed).to be_a(OAuth2::Response::DEFAULT_OPTIONS[:snaky_hash_klass])
    end

    it "doesn't convert to any special hash class when snaky is false" do
      response = described_class.new(@response, parse: :automatic, snaky: false, snaky_hash_klass: custom_hash_class)
      expect(response.parsed).to be_a(Hash)
      expect(response.parsed).not_to be_a(custom_hash_class)
    end
  end

  describe "with dump_value & load_value extensions" do
    let(:custom_hash_class) do
      klass = Class.new(SnakyHash::StringKeyed) do
        # Give this hash class `dump` and `load` abilities!
        extend SnakyHash::Serializer

        unless instance_methods.include?(:transform_keys)
          # Patch our custom Hash to support Ruby < 2.4.2
          def transform_keys!
            keys.each do |key|
              ref = delete(key)
              self[key] = yield(ref)
            end
          end

          def transform_keys
            dup.transform_keys! { |key| yield(key) }
          end
        end
      end

      # Act on the non-hash values as they are dumped to JSON
      klass.dump_value_extensions.add(:to_fruit) do |value|
        "banana"
      end

      # Act on the non-hash values as they are loaded from the JSON dump
      klass.load_value_extensions.add(:to_stars) do |value|
        "asdf***qwer"
      end

      klass
    end

    before do
      @response = double(
        "response",
        headers: {"Content-Type" => "application/json"},
        status: 200,
        body: parsed_response.to_json,
      )
    end

    context "when hash with top-level hashes" do
      let(:parsed_response) { {"a-b_c-d_e-F_G-H" => "i-j_k-l_m-n_o-P_Q-R", "arr" => [1, 2, 3]} }

      it "uses the specified hash class when snaky is true" do
        response = described_class.new(@response, parse: :automatic, snaky: true, snaky_hash_klass: custom_hash_class)
        expect(response.parsed).to be_a(custom_hash_class)
        expect(response.parsed).to eq("a_b_c_d_e_f_g_h" => "i-j_k-l_m-n_o-P_Q-R", "arr" => [1, 2, 3])
        expect(response.parsed["a_b_c_d_e_f_g_h"]).to eq("i-j_k-l_m-n_o-P_Q-R")
        expect(response.parsed[:a_b_c_d_e_f_g_h]).to eq("i-j_k-l_m-n_o-P_Q-R")
        expect(response.parsed.a_b_c_d_e_f_g_h).to eq("i-j_k-l_m-n_o-P_Q-R")
        expect(response.parsed["arr"]).to eq([1, 2, 3])
        expect(response.parsed[:arr]).to eq([1, 2, 3])
        expect(response.parsed.arr).to eq([1, 2, 3])
      end

      it "can dump the hash" do
        response = described_class.new(@response, parse: :automatic, snaky: true, snaky_hash_klass: custom_hash_class)
        expect(response.parsed).to be_a(custom_hash_class)
        expect(response.parsed.class.dump_value_extensions.has?(:to_fruit)).to be(true)
        dump = custom_hash_class.dump(response.parsed)
        expect(dump).to eq("{\"a_b_c_d_e_f_g_h\":\"banana\",\"arr\":[\"banana\",\"banana\",\"banana\"]}")
      end

      it "can load the dump, and run extensions on values" do
        response = described_class.new(@response, parse: :automatic, snaky: true, snaky_hash_klass: custom_hash_class)
        expect(response.parsed).to be_a(custom_hash_class)
        expect(response.parsed.class.load_value_extensions.has?(:to_stars)).to be(true)
        dump = custom_hash_class.dump(response.parsed)
        hydrated = custom_hash_class.load(dump)
        expect(hydrated).not_to eq(response.parsed.to_hash)
        expect(hydrated).to eq({
          "a_b_c_d_e_f_g_h" => "asdf***qwer",
          "arr" => %w[asdf***qwer asdf***qwer asdf***qwer],
        })
        expect(hydrated["a_b_c_d_e_f_g_h"]).to eq("asdf***qwer")
        expect(hydrated[:a_b_c_d_e_f_g_h]).to eq("asdf***qwer")
        expect(hydrated.a_b_c_d_e_f_g_h).to eq("asdf***qwer")
        expect(hydrated["arr"]).to eq(%w[asdf***qwer asdf***qwer asdf***qwer])
        expect(hydrated[:arr]).to eq(%w[asdf***qwer asdf***qwer asdf***qwer])
        expect(hydrated.arr).to eq(%w[asdf***qwer asdf***qwer asdf***qwer])
      end

      it "doesn't convert to any special hash class when snaky is false" do
        response = described_class.new(@response, parse: :automatic, snaky: false, snaky_hash_klass: custom_hash_class)
        expect(response.parsed).to be_a(Hash)
        expect(response.parsed).not_to be_a(custom_hash_class)
        expect(response.parsed).to eq("a-b_c-d_e-F_G-H" => "i-j_k-l_m-n_o-P_Q-R", "arr" => [1, 2, 3])
        expect(response.parsed["a-b_c-d_e-F_G-H"]).to eq("i-j_k-l_m-n_o-P_Q-R")
        expect(response.parsed["arr"]).to eq([1, 2, 3])
      end
    end

    context "when hash with nested hashes" do
      let(:parsed_response) { {"a-b_c-d_e-F_G-H" => {"i-j_k-l_m-n_o-P_Q-R" => "s-t_u-v_w-X_Y-Z"}, "arr" => [1, 2, 3]} }

      it "uses the specified hash class when snaky is true" do
        response = described_class.new(@response, parse: :automatic, snaky: true, snaky_hash_klass: custom_hash_class)
        expect(response.parsed).to be_a(custom_hash_class)
        expect(response.parsed).to eq("a_b_c_d_e_f_g_h" => {"i_j_k_l_m_n_o_p_q_r" => "s-t_u-v_w-X_Y-Z"}, "arr" => [1, 2, 3])
        expect(response.parsed["a_b_c_d_e_f_g_h"]).to eq({"i_j_k_l_m_n_o_p_q_r" => "s-t_u-v_w-X_Y-Z"})
        expect(response.parsed[:a_b_c_d_e_f_g_h]).to eq({"i_j_k_l_m_n_o_p_q_r" => "s-t_u-v_w-X_Y-Z"})
        expect(response.parsed.a_b_c_d_e_f_g_h).to eq({"i_j_k_l_m_n_o_p_q_r" => "s-t_u-v_w-X_Y-Z"})
        expect(response.parsed["arr"]).to eq([1, 2, 3])
        expect(response.parsed[:arr]).to eq([1, 2, 3])
        expect(response.parsed.arr).to eq([1, 2, 3])
      end

      it "can dump the hash" do
        response = described_class.new(@response, parse: :automatic, snaky: true, snaky_hash_klass: custom_hash_class)
        expect(response.parsed).to be_a(custom_hash_class)
        expect(response.parsed.class.dump_value_extensions.has?(:to_fruit)).to be(true)
        dump = custom_hash_class.dump(response.parsed)
        expect(dump).to eq("{\"a_b_c_d_e_f_g_h\":{\"i_j_k_l_m_n_o_p_q_r\":\"banana\"},\"arr\":[\"banana\",\"banana\",\"banana\"]}")
      end

      it "can load the dump, and run extensions on values" do
        response = described_class.new(@response, parse: :automatic, snaky: true, snaky_hash_klass: custom_hash_class)
        expect(response.parsed).to be_a(custom_hash_class)
        expect(response.parsed.class.load_value_extensions.has?(:to_stars)).to be(true)
        dump = custom_hash_class.dump(response.parsed)
        hydrated = custom_hash_class.load(dump)
        expect(hydrated).not_to eq(response.parsed.to_hash)
        expect(hydrated).to eq({
          "a_b_c_d_e_f_g_h" =>
            {
              "i_j_k_l_m_n_o_p_q_r" => "asdf***qwer",
            },
          "arr" => %w[asdf***qwer asdf***qwer asdf***qwer],
        })
        expect(hydrated["a_b_c_d_e_f_g_h"]).to eq({"i_j_k_l_m_n_o_p_q_r" => "asdf***qwer"})
        expect(hydrated[:a_b_c_d_e_f_g_h]).to eq({"i_j_k_l_m_n_o_p_q_r" => "asdf***qwer"})
        expect(hydrated.a_b_c_d_e_f_g_h).to eq({"i_j_k_l_m_n_o_p_q_r" => "asdf***qwer"})
        expect(hydrated["arr"]).to eq(%w[asdf***qwer asdf***qwer asdf***qwer])
        expect(hydrated[:arr]).to eq(%w[asdf***qwer asdf***qwer asdf***qwer])
        expect(hydrated.arr).to eq(%w[asdf***qwer asdf***qwer asdf***qwer])
      end

      it "doesn't convert to any special hash class when snaky is false" do
        response = described_class.new(@response, parse: :automatic, snaky: false, snaky_hash_klass: custom_hash_class)
        expect(response.parsed).to be_a(Hash)
        expect(response.parsed).not_to be_a(custom_hash_class)
        expect(response.parsed).to eq("a-b_c-d_e-F_G-H" => {"i-j_k-l_m-n_o-P_Q-R" => "s-t_u-v_w-X_Y-Z"}, "arr" => [1, 2, 3])
        expect(response.parsed["a-b_c-d_e-F_G-H"]).to eq({"i-j_k-l_m-n_o-P_Q-R" => "s-t_u-v_w-X_Y-Z"})
        expect(response.parsed["arr"]).to eq([1, 2, 3])
      end
    end
  end

  describe "with dump_hash & load_hash extensions" do
    let(:custom_hash_class) do
      klass = Class.new(SnakyHash::StringKeyed) do
        # Give this hash class `dump` and `load` abilities!
        extend SnakyHash::Serializer

        unless instance_methods.include?(:transform_keys)
          # Patch our custom Hash to support Ruby < 2.4.2
          def transform_keys!
            keys.each do |key|
              ref = delete(key)
              self[key] = yield(ref)
            end
          end

          def transform_keys
            dup.transform_keys! { |key| yield(key) }
          end
        end
      end

      # Act on the entire hash as it is prepared for dumping to JSON
      klass.dump_hash_extensions.add(:to_cheese) do |value|
        if value.is_a?(Hash)
          # TODO: Drop this hack when dropping support for Ruby 2.6
          ref = value.transform_keys do |key|
            # This is an example tailored to this specific test!
            # It is not a generalized solution for anything!
            split = key.split("_")
            first_word = split[0]
            key.sub(first_word, "cheese")
          end
          # TODO: Drop this hack when dropping support for Ruby <= 2.4
          if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.4.2")
            ref
          else
            puts
            klass[ref]
          end
        else
          value
        end
      end

      # Act on the entire hash as it is loaded from the JSON dump
      klass.load_hash_extensions.add(:to_pizza) do |value|
        if value.is_a?(Hash)
          # TODO: Drop this hack when dropping support for Ruby <= 2.4
          if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.4.2")
            value.transform_keys do |key|
              # This is an example tailored to this specific test!
              # It is not a generalized solution for anything!
              split = key.split("_")
              last_word = split[-1]
              key.sub(last_word, "pizza")
            end
          else
            res = klass.new
            value.keys.each_with_object(res) do |key, result|
              split = key.split("_")
              last_word = split[-1]
              new_key = key.sub(last_word, "pizza")
              result[new_key] = value[key]
            end
            res
          end
        else
          value
        end
      end

      klass
    end

    before do
      @response = double(
        "response",
        headers: {"Content-Type" => "application/json"},
        status: 200,
        body: parsed_response.to_json,
      )
    end

    context "when hash with top-level hashes" do
      let(:parsed_response) { {"a-b_c-d_e-F_G-H" => "i-j_k-l_m-n_o-P_Q-R", "arr" => [1, 2, 3]} }

      it "uses the specified hash class when snaky is true" do
        response = described_class.new(@response, parse: :automatic, snaky: true, snaky_hash_klass: custom_hash_class)
        expect(response.parsed).to be_a(custom_hash_class)
        expect(response.parsed).to eq("a_b_c_d_e_f_g_h" => "i-j_k-l_m-n_o-P_Q-R", "arr" => [1, 2, 3])
        expect(response.parsed["a_b_c_d_e_f_g_h"]).to eq("i-j_k-l_m-n_o-P_Q-R")
        expect(response.parsed[:a_b_c_d_e_f_g_h]).to eq("i-j_k-l_m-n_o-P_Q-R")
        expect(response.parsed.a_b_c_d_e_f_g_h).to eq("i-j_k-l_m-n_o-P_Q-R")
        expect(response.parsed["arr"]).to eq([1, 2, 3])
        expect(response.parsed[:arr]).to eq([1, 2, 3])
        expect(response.parsed.arr).to eq([1, 2, 3])
      end

      it "can dump the hash" do
        response = described_class.new(@response, parse: :automatic, snaky: true, snaky_hash_klass: custom_hash_class)
        expect(response.parsed).to be_a(custom_hash_class)
        expect(response.parsed.class.dump_hash_extensions.has?(:to_cheese)).to be(true)
        puts "response.parsed: #{response.parsed.inspect} (#{response.parsed})"
        dump = custom_hash_class.dump(response.parsed)
        expect(dump).to eq("{\"cheese_b_c_d_e_f_g_h\":\"i-j_k-l_m-n_o-P_Q-R\",\"cheese\":[1,2,3]}")
      end

      it "can load the dump, and run extensions on values" do
        response = described_class.new(@response, parse: :automatic, snaky: true, snaky_hash_klass: custom_hash_class)
        expect(response.parsed).to be_a(custom_hash_class)
        expect(response.parsed.class.load_hash_extensions.has?(:to_pizza)).to be(true)
        dump = custom_hash_class.dump(response.parsed)
        hydrated = custom_hash_class.load(dump)
        expect(hydrated).not_to eq(response.parsed.to_hash)
        expect(hydrated).to eq({
          "cpizzaeese_b_c_d_e_f_g_h" => "i-j_k-l_m-n_o-P_Q-R",
          "pizza" => [1, 2, 3],
        })
        expect(hydrated["cpizzaeese_b_c_d_e_f_g_h"]).to eq("i-j_k-l_m-n_o-P_Q-R")
        expect(hydrated[:cpizzaeese_b_c_d_e_f_g_h]).to eq("i-j_k-l_m-n_o-P_Q-R")
        expect(hydrated.cpizzaeese_b_c_d_e_f_g_h).to eq("i-j_k-l_m-n_o-P_Q-R")
        expect(hydrated["pizza"]).to eq([1, 2, 3])
        expect(hydrated[:pizza]).to eq([1, 2, 3])
        expect(hydrated.pizza).to eq([1, 2, 3])
      end

      it "doesn't convert to any special hash class when snaky is false" do
        response = described_class.new(@response, parse: :automatic, snaky: false, snaky_hash_klass: custom_hash_class)
        expect(response.parsed).to be_a(Hash)
        expect(response.parsed).not_to be_a(custom_hash_class)
        expect(response.parsed).to eq("a-b_c-d_e-F_G-H" => "i-j_k-l_m-n_o-P_Q-R", "arr" => [1, 2, 3])
        expect(response.parsed["a-b_c-d_e-F_G-H"]).to eq("i-j_k-l_m-n_o-P_Q-R")
        expect(response.parsed["arr"]).to eq([1, 2, 3])
      end
    end

    context "when hash with nested hashes" do
      let(:parsed_response) { {"a-b_c-d_e-F_G-H" => {"i-j_k-l_m-n_o-P_Q-R" => "s-t_u-v_w-X_Y-Z"}, "arr" => [1, 2, 3]} }

      it "uses the specified hash class when snaky is true" do
        response = described_class.new(@response, parse: :automatic, snaky: true, snaky_hash_klass: custom_hash_class)
        expect(response.parsed).to be_a(custom_hash_class)
        expect(response.parsed).to eq("a_b_c_d_e_f_g_h" => {"i_j_k_l_m_n_o_p_q_r" => "s-t_u-v_w-X_Y-Z"}, "arr" => [1, 2, 3])
        expect(response.parsed["a_b_c_d_e_f_g_h"]).to eq({"i_j_k_l_m_n_o_p_q_r" => "s-t_u-v_w-X_Y-Z"})
        expect(response.parsed[:a_b_c_d_e_f_g_h]).to eq({"i_j_k_l_m_n_o_p_q_r" => "s-t_u-v_w-X_Y-Z"})
        expect(response.parsed.a_b_c_d_e_f_g_h).to eq({"i_j_k_l_m_n_o_p_q_r" => "s-t_u-v_w-X_Y-Z"})
        expect(response.parsed["arr"]).to eq([1, 2, 3])
        expect(response.parsed[:arr]).to eq([1, 2, 3])
        expect(response.parsed.arr).to eq([1, 2, 3])
      end

      it "can dump the hash" do
        response = described_class.new(@response, parse: :automatic, snaky: true, snaky_hash_klass: custom_hash_class)
        expect(response.parsed).to be_a(custom_hash_class)
        expect(response.parsed.class.dump_hash_extensions.has?(:to_cheese)).to be(true)
        dump = custom_hash_class.dump(response.parsed)
        expect(dump).to eq("{\"cheese_b_c_d_e_f_g_h\":{\"cheese_j_k_l_m_n_o_p_q_r\":\"s-t_u-v_w-X_Y-Z\"},\"cheese\":[1,2,3]}")
      end

      it "can load the dump, and run extensions on values" do
        response = described_class.new(@response, parse: :automatic, snaky: true, snaky_hash_klass: custom_hash_class)
        expect(response.parsed).to be_a(custom_hash_class)
        expect(response.parsed.class.load_hash_extensions.has?(:to_pizza)).to be(true)
        dump = custom_hash_class.dump(response.parsed)
        hydrated = custom_hash_class.load(dump)
        expect(hydrated).not_to eq(response.parsed.to_hash)
        expect(hydrated).to eq({
          "cpizzaeese_b_c_d_e_f_g_h" => {"cheese_j_k_l_m_n_o_p_q_pizza" => "s-t_u-v_w-X_Y-Z"},
          "pizza" => [1, 2, 3],
        })
        expect(hydrated["cpizzaeese_b_c_d_e_f_g_h"]).to eq({"cheese_j_k_l_m_n_o_p_q_pizza" => "s-t_u-v_w-X_Y-Z"})
        expect(hydrated[:cpizzaeese_b_c_d_e_f_g_h]).to eq({"cheese_j_k_l_m_n_o_p_q_pizza" => "s-t_u-v_w-X_Y-Z"})
        expect(hydrated.cpizzaeese_b_c_d_e_f_g_h).to eq({"cheese_j_k_l_m_n_o_p_q_pizza" => "s-t_u-v_w-X_Y-Z"})
        expect(hydrated["pizza"]).to eq([1, 2, 3])
        expect(hydrated[:pizza]).to eq([1, 2, 3])
        expect(hydrated.pizza).to eq([1, 2, 3])
      end

      it "doesn't convert to any special hash class when snaky is false" do
        response = described_class.new(@response, parse: :automatic, snaky: false, snaky_hash_klass: custom_hash_class)
        expect(response.parsed).to be_a(Hash)
        expect(response.parsed).not_to be_a(custom_hash_class)
        expect(response.parsed).to eq("a-b_c-d_e-F_G-H" => {"i-j_k-l_m-n_o-P_Q-R" => "s-t_u-v_w-X_Y-Z"}, "arr" => [1, 2, 3])
        expect(response.parsed["a-b_c-d_e-F_G-H"]).to eq({"i-j_k-l_m-n_o-P_Q-R" => "s-t_u-v_w-X_Y-Z"})
        expect(response.parsed["arr"]).to eq([1, 2, 3])
      end
    end
  end
end