File: disconnect_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 (120 lines) | stat: -rw-r--r-- 3,305 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
require "spec_helper"

describe "server disconnect" do
  let(:engine) { double "engine" }
  let(:server) { Faye::Server.new }

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

  describe :disconnect do
    let(:client_id) { "fakeclientid" }
    let(:message) { { "channel" => "/meta/disconnect",
                      "clientId" => "fakeclientid"
                  } }

    describe "with valid parameters" do
      before do
        engine.should_receive(:client_exists).with(client_id).and_yield true
      end

      it "destroys the client" do
        engine.should_receive(:destroy_client).with(client_id)
        server.disconnect(message) {}
      end

      it "returns a successful response" do
        engine.stub(:destroy_client)
        server.disconnect(message) do |response|
          response.should == {
            "channel"    => "/meta/disconnect",
            "successful" => true,
            "clientId"   => client_id
          }
        end
      end

      describe "with a message id" do
        before { message["id"] = "foo" }

        it "returns the same id" do
          engine.stub(:destroy_client)
          server.disconnect(message) do |response|
            response.should == {
              "channel"    => "/meta/disconnect",
              "successful" => true,
              "clientId"   => client_id,
              "id"         => "foo"
            }
          end
        end
      end
    end

    describe "with an unknown client" do
      before do
        engine.should_receive(:client_exists).with(client_id).and_yield false
      end

      it "does not destroy the client" do
        engine.should_not_receive(:destroy_client)
        server.disconnect(message) {}
      end

      it "returns an unsuccessful response" do
        server.disconnect(message) do |response|
          response.should == {
            "channel"    => "/meta/disconnect",
            "successful" => false,
            "error"      => "401:fakeclientid:Unknown client"
          }
        end
      end
    end

    describe "missing clientId" do
      before do
        message.delete("clientId")
        engine.should_receive(:client_exists).with(nil).and_yield false
      end

      it "does not destroy the client" do
        engine.should_not_receive(:destroy_client)
        server.disconnect(message) {}
      end

      it "returns an unsuccessful response" do
        server.disconnect(message) do |response|
          response.should == {
            "channel"    => "/meta/disconnect",
            "successful" => false,
            "error"      => "402:clientId:Missing required parameter"
          }
        end
      end
    end

    describe "with an error" do
      before do
        message["error"] = "invalid"
        engine.should_receive(:client_exists).with(client_id).and_yield true
      end

      it "does not destroy the client" do
        engine.should_not_receive(:destroy_client)
        server.disconnect(message) {}
      end

      it "returns an unsuccessful response" do
        server.disconnect(message) do |response|
          response.should == {
            "channel"    => "/meta/disconnect",
            "successful" => false,
            "error"      => "invalid"
          }
        end
      end
    end
  end
end