File: rack_adapter_spec.rb

package info (click to toggle)
ruby-faye 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,792 kB
  • sloc: javascript: 14,833; ruby: 5,068; makefile: 30
file content (280 lines) | stat: -rw-r--r-- 9,958 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
require "spec_helper"

describe Faye::RackAdapter do
  include Rack::Test::Methods

  let(:adapter)  { Faye::RackAdapter.new(settings) { |ra| @yielded = ra } }
  let(:app)      { ServerProxy.new(adapter) }
  let(:settings) { { :mount => "/bayeux", :timeout => 30 } }
  let(:server)   { double "server" }

  after { app.stop }

  let(:content_type) { last_response["Content-Type"] }
  let(:json)         { MultiJson.load(body) }
  let(:body)         { last_response.body }
  let(:status)       { last_response.status.to_i }

  def headers(name)
    last_response[name]
  end

  before do
    Faye::Server.should_receive(:new).with(settings).and_return server
    adapter.stub(:get_client).and_return double("client")
  end

  describe "monitoring configuration" do
    it "should be possible by providing a block to initializer" do
      @yielded.should be_instance_of(Faye::RackAdapter)
    end
  end

  describe "OPTIONS requests" do
    describe "with origin specified" do
      before { header "Origin", "http://example.com" }

      it "returns a matching cross-origin access control header" do
        options "/bayeux"
        headers("Access-Control-Allow-Origin").should == "http://example.com"
        headers("Access-Control-Allow-Credentials").should == "true"
        headers("Access-Control-Allow-Headers").should == "Accept, Authorization, Content-Type, Pragma, X-Requested-With"
        headers("Access-Control-Allow-Methods").should == "POST, GET"
        headers("Access-Control-Max-Age").should == "86400"
      end
    end

    describe "with referer specified" do
      before { header "Referer", "http://example.com" }

      it "returns a matching cross-origin access control header" do
        options "/bayeux"
        headers("Access-Control-Allow-Origin").should == "http://example.com"
        headers("Access-Control-Allow-Credentials").should == "true"
        headers("Access-Control-Allow-Headers").should == "Accept, Authorization, Content-Type, Pragma, X-Requested-With"
        headers("Access-Control-Allow-Methods").should == "POST, GET"
        headers("Access-Control-Max-Age").should == "86400"
      end
    end

    describe "with no origin specified" do
      it "returns a wildcard cross-origin access control header" do
        options "/bayeux"
        headers("Access-Control-Allow-Origin").should == "*"
        headers("Access-Control-Allow-Credentials").should == "true"
        headers("Access-Control-Allow-Headers").should == "Accept, Authorization, Content-Type, Pragma, X-Requested-With"
        headers("Access-Control-Allow-Methods").should == "POST, GET"
        headers("Access-Control-Max-Age").should == "86400"
      end
    end
  end

  describe "POST requests" do
    describe "with cross-origin access control" do
      shared_examples_for "cross-origin request" do
        before do
          header "Origin", "http://example.com"
        end

        it "returns a matching cross-origin access control header" do
          server.stub(:process).and_yield []
          post "/bayeux", :message => '[]'
          headers("Access-Control-Allow-Origin").should == "http://example.com"
        end

        it "forwards the message param onto the server" do
          server.should_receive(:process).with({ "channel" => "/plain" }, instance_of(Rack::Request)).and_yield []
          post "/bayeux", "message=%7B%22channel%22%3A%22%2Fplain%22%7D"
        end

        it "returns the server's response as JSON" do
          server.stub(:process).and_yield ["channel" => "/meta/handshake"]
          post "/bayeux", "message=%5B%5D"
          status.should == 200
          content_type.should == "application/json; charset=utf-8"
          headers("Content-Length").should == "31"
          json.should == ["channel" => "/meta/handshake"]
        end

        it "returns a 400 response if malformed JSON is given" do
          server.should_not_receive(:process)
          post "/bayeux", "message=%7B%5B"
          status.should == 400
          content_type.should == "text/plain; charset=utf-8"
        end

        it "returns a 400 response if primitive JSON is given" do
          server.should_not_receive(:process)
          post "/bayeux", "message=1"
          status.should == 400
          content_type.should == "text/plain; charset=utf-8"
        end

        it "returns a 404 if the path is not matched" do
          server.should_not_receive(:process)
          post "/blaf", 'message=%5B%5D'
          status.should == 404
          content_type.should == "text/plain; charset=utf-8"
        end
      end

      describe "with text/plain" do
        before { header "Content-Type", "text/plain" }
        it_should_behave_like "cross-origin request"
      end

      describe "with application/xml" do
        before { header "Content-Type", "application/xml" }
        it_should_behave_like "cross-origin request"
      end
    end

    describe "with application/json" do
      before do
        header "Content-Type", "application/json"
      end

      it "does not return an access control header" do
        server.stub(:process).and_yield []
        post "/bayeux", :message => '[]'
        headers("Access-Control-Allow-Origin").should be_nil
      end

      it "forwards the POST body onto the server" do
        server.should_receive(:process).with({ "channel" => "/foo" }, instance_of(Rack::Request)).and_yield []
        post "/bayeux", '{ "channel":"/foo" }'
      end

      it "returns the server's response as JSON" do
        server.stub(:process).and_yield ["channel" => "/meta/handshake"]
        post "/bayeux", '[]'
        status.should == 200
        content_type.should == "application/json; charset=utf-8"
          headers("Content-Length").should == "31"
        json.should == ["channel" => "/meta/handshake"]
      end

      it "returns a 400 response if malformed JSON is given" do
        server.should_not_receive(:process)
        post "/bayeux", "[ }"
        status.should == 400
        content_type.should == "text/plain; charset=utf-8"
      end

      it "returns a 404 if the path is not matched" do
        server.should_not_receive(:process)
        post "/blaf", "[]"
        status.should == 404
        content_type.should == "text/plain; charset=utf-8"
      end
    end

    describe "with no content type" do
      it "forwards the message param onto the server" do
        server.should_receive(:process).with({ "channel" => "/foo" }, instance_of(Rack::Request)).and_yield []
        post "/bayeux", :message => '{ "channel":"/foo" }'
      end

      it "returns the server's response as JSON" do
        server.stub(:process).and_yield ["channel" => "/meta/handshake"]
        post "/bayeux", :message => '[]'
        status.should == 200
        content_type.should == "application/json; charset=utf-8"
        headers("Content-Length").should == "31"
        json.should == ["channel" => "/meta/handshake"]
      end

      it "returns a 400 response if malformed JSON is given" do
        server.should_not_receive(:process)
        post "/bayeux", :message => "[ }"
        status.should == 400
        content_type.should == "text/plain; charset=utf-8"
      end

      it "returns a 404 if the path is not matched" do
        server.should_not_receive(:process)
        post "/blaf", :message => "[]"
        status.should == 404
        content_type.should == "text/plain; charset=utf-8"
      end
    end
  end

  describe "GET requests" do
    let(:params) { { :message => '{ "channel":"/foo" }', :jsonp => "callback" } }

    describe "with valid params" do
      it "forwards the message param onto the server" do
        server.should_receive(:process).with({ "channel" => "/foo" }, instance_of(Rack::Request)).and_yield []
        get "/bayeux", params
      end

      it "returns the server's response as JavaScript" do
        server.stub(:process).and_yield ["channel" => "/meta/handshake"]
        get "/bayeux", params
        status.should == 200
        content_type.should == "text/javascript; charset=utf-8"
        headers("Content-Length").should == "46"
        body.should == '/**/callback([{"channel":"/meta/handshake"}]);'
      end

      it "does not let the client cache the response" do
        server.stub(:process).and_yield ["channel" => "/meta/handshake"]
        get "/bayeux", params
        headers("Cache-Control").should == "no-cache, no-store"
      end
    end

    describe "with an unknown path" do
      it "returns a 404" do
        server.should_not_receive(:process)
        get "/blah", params
        status.should == 404
        content_type.should == "text/plain; charset=utf-8"
      end
    end

    describe "missing jsonp" do
      before do
        params.delete(:jsonp)
      end

      it "returns the server's response using the default callback" do
        server.stub(:process).and_yield ["channel" => "/meta/handshake"]
        get "/bayeux", params
        status.should == 200
        content_type.should == "text/javascript; charset=utf-8"
        headers("Content-Length").should == "51"
        body.should == '/**/jsonpcallback([{"channel":"/meta/handshake"}]);'
      end
    end

    shared_examples_for "bad GET request" do
      it "does not call the server" do
        server.should_not_receive(:process)
        get "/bayeux", params
      end

      it "returns a 400 response" do
        get "/bayeux", params
        status.should == 400
        content_type.should == "text/plain; charset=utf-8"
      end
    end

    describe "with malformed JSON" do
      before { params[:message] = "[ }" }
      it_should_behave_like "bad GET request"
    end

    describe "with a callback that's not a JS identifier" do
      before { params[:jsonp] = "42" }
      it_should_behave_like "bad GET request"
    end

    describe "missing message" do
      before { params.delete(:message) }
      it_should_behave_like "bad GET request"
    end
  end
end