File: sham_rack_spec.rb

package info (click to toggle)
ruby-sham-rack 1.3.6-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 176 kB
  • ctags: 62
  • sloc: ruby: 658; makefile: 4
file content (371 lines) | stat: -rw-r--r-- 8,493 bytes parent folder | download | duplicates (2)
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
require "spec_helper"

require "sham_rack"
require "open-uri"
require "restclient"
require "mechanize"
require "rack"

describe ShamRack do

  class NetHttpProhibited < StandardError; end

  before do
    any_instance_of(Net::HTTP) do |http|
      stub(http).start do
        raise NetHttpProhibited, "real network calls are not allowed"
      end
    end
  end

  after(:each) do
    ShamRack.unmount_all
  end

  describe "mounted Rack application" do

    before(:each) do
      ShamRack.mount(GreetingApp.new, "www.greetings.com")
    end

    it "can be accessed using Net::HTTP" do
      response = Net::HTTP.start("www.greetings.com") do |http|
        http.request(Net::HTTP::Get.new("/"))
      end
      response.body.should == "Hello, world"
    end

    it "can be accessed using Net::HTTP#get_response" do
      response = Net::HTTP.get_response(URI.parse("http://www.greetings.com/"))
      response.body.should == "Hello, world"
    end

    it "can be accessed using open-uri" do
      response = open("http://www.greetings.com")
      response.status.should == ["200", "OK"]
      response.read.should == "Hello, world"
    end

    it "can be accessed using RestClient" do
      response = RestClient.get("http://www.greetings.com")
      response.code.should == 200
      response.to_s.should == "Hello, world"
    end

    it "can be accessed using Mechanize" do
      response = Mechanize.new.get("http://www.greetings.com")
      response.body.should == "Hello, world"
    end

    xit "can be accessed using Patron" do
      patron = Patron::Session.new
      response = patron.get("http://www.greetings.com/foo/bar")
      response.body.should == "Hello, world"
    end

  end

  describe ".at" do

    context "with a block" do

      it "mounts associated block as an app" do

        ShamRack.at("simple.xyz") do |env|
          ["200 OK", { "Content-type" => "text/plain" }, ["Easy, huh?"]]
        end

        open("http://simple.xyz").read.should == "Easy, huh?"

      end

    end

    context "with a URL" do

      it "raises an ArgumentError" do
        lambda do
          ShamRack.at("http://www.greetings.com")
        end.should raise_error(ArgumentError, "invalid address")
      end

    end

    describe "#mount" do

      it "mounts an app" do

        ShamRack.at("hello.xyz").mount(GreetingApp.new)

        open("http://hello.xyz").read.should == "Hello, world"

      end

    end

    describe "#unmount" do

      it "deregisters a mounted app" do

        ShamRack.at("gone.xyz").mount(GreetingApp.new)
        ShamRack.at("gone.xyz").unmount

        lambda do
          open("http://gone.xyz").read
        end.should raise_error(NetHttpProhibited)

      end

    end

    describe "#rackup" do

      before do
        @return_value = ShamRack.at("rackup.xyz").rackup do
          use UpcaseBody
          run GreetingApp.new
        end
      end

      it "mounts an app created using Rack::Builder" do
        open("http://rackup.xyz").read.should == "HELLO, WORLD"
      end

      it "returns the app" do
        @return_value.should respond_to(:call)
      end

    end

    describe "#sinatra" do

      before do
        @return_value = ShamRack.at("sinatra.xyz").sinatra do
          get "/hello/:subject" do
            "Hello, #{params[:subject]}"
          end
        end
      end

      it "mounts associated block as a Sinatra app" do
        open("http://sinatra.xyz/hello/stranger").read.should == "Hello, stranger"
      end

      it "returns the app" do
        @return_value.should respond_to(:call)
      end

    end

    describe "#stub" do

      before do
        @return_value = ShamRack.at("stubbed.xyz").stub
      end

      it "mounts a StubWebService" do
        ShamRack.application_for("stubbed.xyz").should be_kind_of(ShamRack::StubWebService)
      end

      it "returns the StubWebService" do
        @return_value.should == ShamRack.application_for("stubbed.xyz")
      end

    end

  end

  describe ".mount" do

    it "is deprecated, but still works" do

      ShamRack.mount(GreetingApp.new, "hello.xyz")

      open("http://hello.xyz").read.should == "Hello, world"

    end

  end

  describe "response" do

    before(:each) do
      ShamRack.at("www.greetings.com") do
        [
          "456 Foo Bar",
          { "Content-Type" => "text/plain", "X-Foo" => "bar" },
          ["BODY"]
        ]
      end
    end

    let(:response) { Net::HTTP.get_response(URI.parse("http://www.greetings.com/")) }

    it "has status returned by app" do
      response.code.should == "456"
    end

    it "has status message returned by app" do
      response.message.should == "Foo Bar"
    end

    it "has body returned by app" do
      response.body.should == "BODY"
    end

    it "has Content-Type returned by app" do
      response.content_type.should == "text/plain"
    end

    it "has other headers returned by app" do
      response["x-foo"].should =="bar"
    end

    context "when the app returns a numeric status" do

      before(:each) do
        ShamRack.at("www.greetings.com") do
          [
            201,
            { "Content-Type" => "text/plain" },
            ["BODY"]
          ]
        end
        @response = Net::HTTP.get_response(URI.parse("http://www.greetings.com/"))
      end

      it "has status returned by app" do
        response.code.should == "201"
      end

      it "derives a status message" do
        response.message.should == "Created"
      end

    end

  end

  describe "Rack environment" do

    before(:each) do
      @env_recorder = recorder = EnvRecorder.new(GreetingApp.new)
      ShamRack.at("env.xyz").rackup do
        use Rack::Lint
        run recorder
      end
    end

    def env
      @env_recorder.last_env
    end

    it "is valid" do

      open("http://env.xyz/blah?q=abc")

      env["REQUEST_METHOD"].should == "GET"
      env["SCRIPT_NAME"].should == ""
      env["PATH_INFO"].should == "/blah"
      env["QUERY_STRING"].should == "q=abc"
      env["SERVER_NAME"].should == "env.xyz"
      env["SERVER_PORT"].should == "80"

      env["rack.version"].should be_kind_of(Array)
      env["rack.url_scheme"].should == "http"

      env["rack.multithread"].should == true
      env["rack.multiprocess"].should == true
      env["rack.run_once"].should == false

    end

    it "provides request headers" do

      Net::HTTP.start("env.xyz") do |http|
        request = Net::HTTP::Get.new("/")
        request["Foo-bar"] = "baz"
        http.request(request)
      end

      env["HTTP_FOO_BAR"].should == "baz"

    end

    it "supports POST" do

      RestClient.post("http://env.xyz/resource", "q" => "rack")

      env["REQUEST_METHOD"].should == "POST"
      env["CONTENT_TYPE"].should == "application/x-www-form-urlencoded"
      env["rack.input"].read.should == "q=rack"

    end

    it "supports POST using Net::HTTP" do

      Net::HTTP.start("env.xyz") do |http|
        http.post("/resource", "q=rack")
      end

      env["REQUEST_METHOD"].should == "POST"
      env["rack.input"].read.should == "q=rack"

    end

    xit "supports POST using Patron" do

      patron = Patron::Session.new
      response = patron.post("http://env.xyz/resource", "<xml/>", "Content-Type" => "application/xml")

      response.status.should == 200

      env["REQUEST_METHOD"].should == "POST"
      env["rack.input"].read.should == "<xml/>"
      env["CONTENT_TYPE"].should == "application/xml"

    end

    it "supports PUT" do

      RestClient.put("http://env.xyz/thing1", "stuff", :content_type => "text/plain")

      env["REQUEST_METHOD"].should == "PUT"
      env["CONTENT_TYPE"].should == "text/plain"
      env["rack.input"].read.should == "stuff"

    end

    xit "supports PUT using Patron" do

      patron = Patron::Session.new
      response = patron.put("http://env.xyz/resource", "stuff", "Content-Type" => "text/plain")

      env["REQUEST_METHOD"].should == "PUT"
      env["CONTENT_TYPE"].should == "text/plain"
      env["rack.input"].read.should == "stuff"

    end

    it "supports DELETE" do

      RestClient.delete("http://env.xyz/thing/1")

      env["REQUEST_METHOD"].should == "DELETE"
      env["PATH_INFO"].should == "/thing/1"

    end

    xit "supports DELETE using Patron" do

      patron = Patron::Session.new
      response = patron.delete("http://env.xyz/resource")

      env["REQUEST_METHOD"].should == "DELETE"
      env["PATH_INFO"].should == "/resource"

    end

  end

end