File: padding_by_user_agent_spec.rb

package info (click to toggle)
libnginx-mod-http-push-stream 0.6.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,272 kB
  • sloc: ruby: 7,101; ansic: 6,048; javascript: 2,121; sh: 53; makefile: 16
file content (207 lines) | stat: -rw-r--r-- 8,672 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
require 'spec_helper'

describe "Subscriber Padding by user agent" do
  let(:default_config) do
    {
      :padding_by_user_agent => "[T|t]est 1,0,508",
      :user_agent => nil,
      :subscriber_connection_ttl => '1s',
      :header_template => nil,
      :message_template => nil,
      :footer_template => nil
    }
  end

  shared_examples_for "apply padding" do
    it "should apply a padding to the header" do
      channel = 'ch_test_header_padding'

      nginx_run_server(config.merge(:header_template => "0123456789", :padding_by_user_agent => "[T|t]est 1,1024,508:[T|t]est 2,4097,0")) do |conf|
        EventMachine.run do
          expected_size = conf.header_template.size + header_delta

          sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
          sub_1.callback do
            expect(sub_1).to be_http_status(200)
            expect(sub_1.response.size).to eql(1100 + expected_size)
            expect(sub_1.response).to match padding_pattern

            sub_2 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 2")
            sub_2.callback do
              expect(sub_2).to be_http_status(200)
              expect(sub_2.response.size).to eql(4097 + expected_size)

              sub_3 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 3")
              sub_3.callback do
                expect(sub_3).to be_http_status(200)
                expect(sub_3.response.size).to eql(expected_size)

                EventMachine.stop
              end
            end
          end
        end
      end
    end

    it "should apply a padding to the message" do
      channel = 'ch_test_message_padding'

      body = "0123456789"

      nginx_run_server(config) do |conf|
        EventMachine.run do
          expected_size = body.size + header_delta + body_delta

          sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
          sub_1.callback {
            expect(sub_1).to be_http_status(200)
            expect(sub_1.response.size).to eql(500 + expected_size)
            expect(sub_1.response).to match padding_pattern

            sub_2 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 2")
            sub_2.callback {
              expect(sub_2).to be_http_status(200)
              expect(sub_2.response.size).to eql(expected_size)

              sub_3 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 3")
              sub_3.callback {
                expect(sub_3).to be_http_status(200)
                expect(sub_3.response.size).to eql(expected_size)

                EventMachine.stop
              }
              publish_message_inline(channel, headers, body)
            }
            publish_message_inline(channel, headers, body)
          }
          publish_message_inline(channel, headers, body)
        end
      end
    end

    it "should apply a padding to the message with different sizes" do
      channel = 'ch_test_message_padding_with_different_sizes'

      nginx_run_server(config.merge(:padding_by_user_agent => "[T|t]est 1,0,545"), :timeout => 10) do |conf|
        EventMachine.run do
          i = 1
          expected_padding = 545
          expected_size = header_delta + body_delta

          sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
          sub_1.callback do
            expect(sub_1).to be_http_status(200)
            expect(sub_1.response.size).to eql(expected_padding + i + expected_size)
            expect(sub_1.response).to match padding_pattern

            i = 105
            expected_padding = 600 - ((i/100).to_i * 100)

            sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
            sub_1.callback do
              expect(sub_1).to be_http_status(200)
              expect(sub_1.response.size).to eql(expected_padding + i + expected_size)

              i = 221
              expected_padding = 600 - ((i/100).to_i * 100)

              sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
              sub_1.callback do
                expect(sub_1).to be_http_status(200)
                expect(sub_1.response.size).to eql(expected_padding + i + expected_size)

                i = 331
                expected_padding = 600 - ((i/100).to_i * 100)

                sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
                sub_1.callback do
                  expect(sub_1).to be_http_status(200)
                  expect(sub_1.response.size).to eql(expected_padding + i + expected_size)

                  i = 435
                  expected_padding = 600 - ((i/100).to_i * 100)

                  sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
                  sub_1.callback do
                    expect(sub_1).to be_http_status(200)
                    expect(sub_1.response.size).to eql(expected_padding + i + expected_size)

                    i = 502
                    expected_padding = 600 - ((i/100).to_i * 100)

                    sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
                    sub_1.callback do
                      expect(sub_1).to be_http_status(200)
                      expect(sub_1.response.size).to eql(expected_padding + i + expected_size)

                      i = 550

                      sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
                      sub_1.callback do
                        expect(sub_1).to be_http_status(200)
                        expect(sub_1.response.size).to eql(i + expected_size)

                        EventMachine.stop
                      end
                      publish_message_inline(channel, headers, "_" * i)
                    end
                    publish_message_inline(channel, headers, "_" * i)
                  end
                  publish_message_inline(channel, headers, "_" * i)
                end
                publish_message_inline(channel, headers, "_" * i)
              end
              publish_message_inline(channel, headers, "_" * i)
            end
            publish_message_inline(channel, headers, "_" * i)
          end
          publish_message_inline(channel, headers, "_" * i)
        end
      end
    end

    it "should accept the user agent set by a complex value" do
      channel = 'ch_test_user_agent_by_complex_value'

      nginx_run_server(config.merge(:padding_by_user_agent => "[T|t]est 1,1024,512", :user_agent => "$arg_ua", :header_template => "0123456789"), :timeout => 10) do |conf|
        EventMachine.run do
          expected_size = conf.header_template.size + header_delta

          sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s + '?ua=test%201').get :head => headers
          sub_1.callback do
            expect(sub_1).to be_http_status(200)
            expect(sub_1.response.size).to eql(1024 + expected_size)
            expect(sub_1.response).to match padding_pattern

            sub_2 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s + '?ua=test%202').get :head => headers
            sub_2.callback do
              expect(sub_2).to be_http_status(200)
              expect(sub_2.response.size).to eql(expected_size)

              EventMachine.stop
            end
          end
        end
      end
    end
  end

  describe "for non EventSource mode" do
    let(:config) { default_config }
    let(:padding_pattern) { /(\r\n)+\r\n\r\n\r\n$/ }
    let(:header_delta) { 0 }
    let(:body_delta) { 0 }

    it_should_behave_like "apply padding"
  end

  describe "for EventSource mode" do
    let(:config) { default_config.merge(:subscriber_mode => "eventsource") }
    let(:padding_pattern) { /(:::)+\n$/ }
    let(:header_delta) { 3 }
    let(:body_delta) { 8 }

    it_should_behave_like "apply padding"
  end
end