aboutsummaryrefslogtreecommitdiffstats
path: root/test/plum/client/test_response.rb
blob: 511a073be0b37f074dff780654c6450632fe1d73 (plain)
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
require "test_helper"

using Plum::BinaryString
class ResponseTest < Minitest::Test
  def test_finished
    resp = Response.new
    resp._headers({})
    assert_equal(false, resp.finished?)
    resp._finish
    assert_equal(true, resp.finished?)
  end

  def test_fail
    resp = Response.new
    resp._fail
    assert(true, resp.failed?)
  end

  def test_status
    resp = Response.new
    resp._headers([
      [":status", "200"]
    ])
    assert_equal("200", resp.status)
  end

  def test_headers
    resp = Response.new
    resp._headers([
      [":status", "200"],
      ["header", "abc"]
    ])
    assert_equal("abc", resp[:HEADER])
  end

  def test_body
    resp = Response.new
    resp._headers({})
    resp._chunk("a")
    resp._chunk("b")
    resp._finish
    assert_equal("ab", resp.body)
  end

  def test_body_not_finished
    resp = Response.new
    resp._headers({})
    resp._chunk("a")
    resp._chunk("b")
    assert_raises { # TODO
      resp.body
    }
  end

  def test_on_chunk
    resp = Response.new
    resp._headers({})
    res = []
    resp._chunk("a")
    resp._chunk("b")
    resp._finish
    resp.on_chunk { |chunk| res << chunk }
    assert_equal(["a", "b"], res)
    resp._chunk("c")
    assert_equal(["a", "b", "c"], res)
  end

  def test_on_finish
    resp = Response.new
    resp._headers({})
    ran = false
    resp.on_finish { ran = true }
    resp._finish
    assert(ran)
    ran = false
    resp.on_finish { ran = true }
    assert(ran)
  end
end