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

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

  def test_fail
    resp = Response.new
    ret = ""
    run = false
    t = Thread.new {
      assert_raises {
        run = true
        resp.each_chunk { |chunk| ret << chunk } } }
    resp._chunk("a")
    resp._fail(RuntimeError.new)
    timeout(3) {
      t.join }
    assert(run)
    assert(true, resp.failed?)
  rescue Timeout::Error
    t.kill
    flunk "timeout"
  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._chunk("a")
    resp._chunk("b")
    resp._finish
    assert_equal("ab", resp.body)
  end

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