File: extensions_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 (103 lines) | stat: -rw-r--r-- 3,146 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
require "spec_helper"

describe "server extensions" do
  let(:engine) do
    engine = double "engine"
    engine.stub(:interval).and_return(0)
    engine.stub(:timeout).and_return(60)
    engine
  end

  let(:server)  { Faye::Server.new }
  let(:message) { { "channel" => "/foo", "data" => "hello" } }

  before do
    Faye::Engine.stub(:get).and_return engine
  end

  describe "with an incoming extension installed" do
    before do
      extension = Class.new do
        def incoming(message, callback)
          message["ext"] = { "auth" => "password" }
          callback.call(message)
        end
      end
      server.add_extension(extension.new)
    end

    it "passes incoming messages through the extension" do
      engine.should_receive(:publish).with({ "channel" => "/foo", "data" => "hello", "ext" => { "auth" => "password" }})
      server.process(message, false) {}
    end

    it "does not pass outgoing messages through the extension" do
      server.stub(:handshake).and_yield(message)
      engine.stub(:publish)
      response = nil
      server.process({ "channel" => "/meta/handshake" }, false) { |r| response = r }
      response.should == [{ "channel" => "/foo", "data" => "hello" }]
    end
  end

  describe "with subscription auth installed" do
    before do
      extension = Class.new do
        def incoming(message, callback)
          if message["channel"] == "/meta/subscribe" and !message["auth"]
            message["error"] = "Invalid auth"
          end
          callback.call(message)
        end
      end
      server.add_extension(extension.new)
    end

    it "does not subscribe using the intended channel" do
      message = {
        "channel" => "/meta/subscribe",
        "clientId" => "fakeclientid",
        "subscription" => "/foo"
      }
      engine.stub(:client_exists).and_yield(true)
      engine.should_not_receive(:subscribe)
      server.process(message, false) {}
    end

    it "does not subscribe using an extended channel" do
      message = {
        "channel" => "/meta/subscribe/x",
        "clientId" => "fakeclientid",
        "subscription" => "/foo"
      }
      engine.stub(:client_exists).and_yield(true)
      engine.should_not_receive(:subscribe)
      server.process(message, false) {}
    end
  end

  describe "with an outgoing extension installed" do
    before do
      extension = Class.new do
        def outgoing(message, callback)
          message["ext"] = { "auth" => "password" }
          callback.call(message)
        end
      end
      server.add_extension(extension.new)
    end

    it "does not pass incoming messages through the extension" do
      engine.should_receive(:publish).with({ "channel" => "/foo", "data" => "hello" })
      server.process(message, false) {}
    end

    it "passes outgoing messages through the extension" do
      server.stub(:handshake).and_yield(message)
      engine.stub(:publish)
      response = nil
      server.process({ "channel" => "/meta/handshake" }, false) { |r| response = r }
      response.should == [{ "channel" => "/foo", "data" => "hello", "ext" => { "auth" => "password" }}]
    end
  end
end