File: stub_request_snippet_spec.rb

package info (click to toggle)
ruby-webmock 3.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,172 kB
  • sloc: ruby: 12,829; makefile: 6
file content (115 lines) | stat: -rw-r--r-- 5,551 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
require 'spec_helper'

describe WebMock::StubRequestSnippet do
  describe "to_s" do
    describe "GET" do
      before(:each) do
        @request_signature = WebMock::RequestSignature.new(:get, "www.example.com/?a=b&c=d", headers: {})
      end

      it "should print stub request snippet with url with params and method and empty successful response" do
        expected = %Q(stub_request(:get, "http://www.example.com/?a=b&c=d").\n  to_return(status: 200, body: "", headers: {}))
        @request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
        expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected)
      end

      it "should print stub request snippet with body if available" do
        @request_signature.body = "abcdef"
        expected = %Q(stub_request(:get, "http://www.example.com/?a=b&c=d").)+
        "\n  with(\n    body: \"abcdef\")." +
        "\n  to_return(status: 200, body: \"\", headers: {})"
        @request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
        expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected)
      end

      it "should print stub request snippet with multiline body" do
        @request_signature.body = "abc\ndef"
        expected = %Q(stub_request(:get, "http://www.example.com/?a=b&c=d").)+
        "\n  with(\n    body: \"abc\\ndef\")." +
        "\n  to_return(status: 200, body: \"\", headers: {})"
        @request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
        expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected)
      end

      it "should print stub request snippet with headers if any" do
        @request_signature.headers = {'B' => 'b', 'A' => 'a'}
        expected = 'stub_request(:get, "http://www.example.com/?a=b&c=d").'+
        "\n  with(\n    headers: {\n\t\  'A\'=>\'a\',\n\t  \'B\'=>\'b\'\n    })." +
        "\n  to_return(status: 200, body: \"\", headers: {})"
        @request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
        expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected)
      end

      it "should print stub request snippet with body and headers" do
        @request_signature.body = "abcdef"
        @request_signature.headers = {'B' => 'b', 'A' => 'a'}
        expected = 'stub_request(:get, "http://www.example.com/?a=b&c=d").'+
        "\n  with(\n    body: \"abcdef\",\n    headers: {\n\t  \'A\'=>\'a\',\n\t  \'B\'=>\'b\'\n    })." +
        "\n  to_return(status: 200, body: \"\", headers: {})"
        @request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
        expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected)
      end

      it "should not print to_return part if not wanted" do
        expected = 'stub_request(:get, "http://www.example.com/").'+
        "\n  with(\n    body: \"abcdef\")"
        stub = WebMock::RequestStub.new(:get, "www.example.com").with(body: "abcdef").to_return(body: "hello")
        expect(WebMock::StubRequestSnippet.new(stub).to_s(false)).to eq(expected)
      end
    end

    describe "POST" do
      let(:form_body) { 'user%5bfirst_name%5d=Bartosz' }
      let(:multipart_form_body) { 'complicated stuff--ABC123--goes here' }
      it "should print stub request snippet with body as a hash using rails conventions on form posts" do
        @request_signature = WebMock::RequestSignature.new(:post, "www.example.com",
                   headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
                   body: form_body)
        @request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
        expected = <<-STUB
stub_request(:post, "http://www.example.com/").
  with(
    body: #{{"user" => {"first_name" => "Bartosz"}}},
    headers: {
\t  'Content-Type'=>'application/x-www-form-urlencoded'
    }).
  to_return(status: 200, body: \"\", headers: {})
        STUB
        expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected.strip)
      end

      it "should print stub request snippet leaving body as string when not a urlencoded form" do
        @request_signature = WebMock::RequestSignature.new(:post, "www.example.com",
                   headers: {'Content-Type' => 'multipart/form-data; boundary=ABC123'},
                   body: multipart_form_body)
        @request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
        expected = <<-STUB
stub_request(:post, "http://www.example.com/").
  with(
    body: "#{multipart_form_body}",
    headers: {
\t  'Content-Type'=>'multipart/form-data; boundary=ABC123'
    }).
  to_return(status: 200, body: \"\", headers: {})
        STUB
        expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected.strip)
      end

      it "should print stub request snippet with valid JSON body when request header contains 'Accept'=>'application/json' " do
        @request_signature = WebMock::RequestSignature.new(:post, "www.example.com",
                   headers: {'Accept' => 'application/json'})
        @request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
        expected = <<-STUB
stub_request(:post, "http://www.example.com/").
  with(
    headers: {
\t  'Accept'=>'application/json'
    }).
  to_return(status: 200, body: \"{}\", headers: {})
        STUB
        expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected.strip)
      end
    end

  end
end