File: headers_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (241 lines) | stat: -rw-r--r-- 7,158 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
require "spec"
require "http/headers"

describe HTTP::Headers do
  it "is empty" do
    headers = HTTP::Headers.new
    headers.empty?.should be_true
  end

  it "is case insensitive" do
    headers = HTTP::Headers{"Foo" => "bar"}
    headers["foo"].should eq("bar")
  end

  it "it allows indifferent access for underscore and dash separated keys" do
    headers = HTTP::Headers{"foo_Bar" => "bar", "Foobar-foo" => "baz"}
    headers["foo-bar"].should eq("bar")
    headers["foobar_foo"].should eq("baz")
  end

  it "raises an error if header value contains invalid character" do
    expect_raises ArgumentError do
      HTTP::Headers{"invalid-header" => "\r\nLocation: http://example.com"}
    end
  end

  it "should retain the input casing" do
    headers = HTTP::Headers{"FOO_BAR" => "bar", "Foobar-foo" => "baz"}
    serialized = String.build do |io|
      headers.each do |name, values|
        io << name << ": " << values.first << ';'
      end
    end

    serialized.should eq("FOO_BAR: bar;Foobar-foo: baz;")
  end

  it "is gets with []?" do
    headers = HTTP::Headers.new
    headers["foo"]?.should be_nil

    headers["Foo"] = "bar"
    headers["foo"]?.should eq("bar")
  end

  it "fetches with default value" do
    headers = HTTP::Headers.new
    headers.fetch("foo", "baz").should eq("baz")

    headers["Foo"] = "bar"
    headers.fetch("foo", "baz").should eq("bar")
  end

  it "fetches with block" do
    headers = HTTP::Headers.new
    headers.fetch("foo") { |k| "#{k}baz" }.should eq("foobaz")

    headers["Foo"] = "bar"
    headers.fetch("foo") { "baz" }.should eq("bar")
  end

  it "has key" do
    headers = HTTP::Headers{"Foo" => "bar"}
    headers.has_key?("foo").should be_true
    headers.has_key?("bar").should be_false
  end

  it "deletes" do
    headers = HTTP::Headers{"Foo" => "bar"}
    headers.delete("foo").should eq("bar")
    headers.should be_empty
  end

  describe "#==" do
    it "equals other instance" do
      a = HTTP::Headers{"Foo" => "bar"}
      b = HTTP::Headers{"Foo" => "bar"}
      c = HTTP::Headers{"Foo" => "baz"}
      a.should eq b
      a.hash.should eq b.hash
      a.should_not eq c
      a.hash.should_not eq c.hash
    end

    it "case-insensitive keys" do
      a = HTTP::Headers{"Foo" => "bar"}
      b = HTTP::Headers{"foo" => "bar"}
      c = HTTP::Headers{"voo" => "bar"}
      a.should eq b
      a.hash.should eq b.hash
      a.should_not eq c
      a.hash.should_not eq c.hash
    end

    it "different internal representation" do
      a = HTTP::Headers{"Foo" => "bar"}
      b = HTTP::Headers{"Foo" => ["bar"]}
      c = HTTP::Headers{"Foo" => ["bar", "baz"]}
      a.should eq b
      a.hash.should eq b.hash
      a.should_not eq c
      a.hash.should_not eq c.hash
    end
  end

  it "dups" do
    headers = HTTP::Headers{"Foo" => "bar"}
    other = headers.dup
    other.should be_a(HTTP::Headers)
    other["foo"].should eq("bar")

    other["Baz"] = "Qux"
    headers["baz"]?.should be_nil
  end

  it "clones" do
    headers = HTTP::Headers{"Foo" => "bar"}
    other = headers.clone
    other.should be_a(HTTP::Headers)
    other["foo"].should eq("bar")

    other["Baz"] = "Qux"
    headers["baz"]?.should be_nil
  end

  it "adds string" do
    headers = HTTP::Headers.new
    headers.add("foo", "bar")
    headers.add("foo", "baz")
    headers["foo"].should eq("bar,baz")
  end

  it "adds array of string" do
    headers = HTTP::Headers.new
    headers.add("foo", "bar")
    headers.add("foo", ["baz", "qux"])
    headers["foo"].should eq("bar,baz,qux")
  end

  it "gets all values" do
    headers = HTTP::Headers{"foo" => "bar"}
    headers.get("foo").should eq(["bar"])

    headers.get?("foo").should eq(["bar"])
    headers.get?("qux").should be_nil
  end

  it "does to_s" do
    headers = HTTP::Headers{"Foo_quux" => "bar", "Baz-Quux" => ["a", "b"]}
    headers.to_s.should eq(%(HTTP::Headers{"Foo_quux" => "bar", "Baz-Quux" => ["a", "b"]}))
  end

  it "#serialize" do
    headers = HTTP::Headers{"Foo_quux" => "bar", "Baz-Quux" => ["a", "b"]}
    headers.serialize.should eq("Foo_quux: bar\r\nBaz-Quux: a\r\nBaz-Quux: b\r\n")
  end

  it "merges and return self" do
    headers = HTTP::Headers.new
    headers.should be headers.merge!({"foo" => "bar"})
  end

  it "matches word" do
    headers = HTTP::Headers{"foo" => "bar"}
    headers.includes_word?("foo", "bar").should be_true
    headers.includes_word?("foo", "ba").should be_false
    headers.includes_word?("foo", "ar").should be_false
  end

  it "matches word with comma separated value" do
    headers = HTTP::Headers{"foo" => "bar, baz"}
    headers.includes_word?("foo", "bar").should be_true
    headers.includes_word?("foo", "baz").should be_true
    headers.includes_word?("foo", "ba").should be_false
  end

  it "matches word with comma separated value, case insensitive (#3626)" do
    headers = HTTP::Headers{"foo" => "BaR, BAZ"}
    headers.includes_word?("foo", "bar").should be_true
    headers.includes_word?("foo", "baz").should be_true
    headers.includes_word?("foo", "BAR").should be_true
    headers.includes_word?("foo", "ba").should be_false
  end

  it "doesn't match empty string" do
    headers = HTTP::Headers{"foo" => "bar, baz"}
    headers.includes_word?("foo", "").should be_false
  end

  it "matches word with comma separated value, partial match" do
    headers = HTTP::Headers{"foo" => "bar, bazo, baz"}
    headers.includes_word?("foo", "baz").should be_true
  end

  it "doesn't match word with comma separated value, partial match" do
    headers = HTTP::Headers{"foo" => "bar, bazo"}
    headers.includes_word?("foo", "baz").should be_false
  end

  it "matches word with comma separated value, partial match (array)" do
    headers = HTTP::Headers{"foo" => ["foo", "baz, bazo"]}
    headers.includes_word?("foo", "baz").should be_true
  end

  it "doesn't match word with comma separated value, partial match (array)" do
    headers = HTTP::Headers{"foo" => ["foo", "bar, bazo"]}
    headers.includes_word?("foo", "baz").should be_false
  end

  it "matches word among headers" do
    headers = HTTP::Headers.new
    headers.add("foo", "bar")
    headers.add("foo", "baz")
    headers.includes_word?("foo", "bar").should be_true
    headers.includes_word?("foo", "baz").should be_true
  end

  it "does not matches word if missing header" do
    headers = HTTP::Headers.new
    headers.includes_word?("foo", "bar").should be_false
    headers.includes_word?("foo", "").should be_false
  end

  it "can create header value with all US-ASCII visible chars (#2999)" do
    headers = HTTP::Headers.new
    value = (32..126).map(&.chr).join
    headers.add("foo", value)
  end

  it "validates content" do
    headers = HTTP::Headers.new
    valid_value = "foo"
    invalid_value = "\r\nLocation: http://example.com"
    headers.valid_value?(valid_value).should be_true
    headers.valid_value?(invalid_value).should be_false
    headers.add?("foo", valid_value).should be_true
    headers.add?("foo", [valid_value]).should be_true
    headers.add?("foobar", invalid_value).should be_false
    headers.add?("foobar", [invalid_value]).should be_false
  end
end