File: test_jsonclient.rb

package info (click to toggle)
ruby-httpclient 2.8.3%2Bgit20211122.4658227-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,908 kB
  • sloc: ruby: 9,963; makefile: 10; sh: 2
file content (98 lines) | stat: -rw-r--r-- 2,981 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
# -*- encoding: utf-8 -*-
require File.expand_path('helper', File.dirname(__FILE__))
require 'jsonclient'


class TestJSONClient < Test::Unit::TestCase
  include Helper

  def setup
    super
    setup_server
    @client = JSONClient.new
  end

  def teardown
    super
  end

  def test_post
    res = @client.post(serverurl + 'json', {'a' => 1, 'b' => {'c' => 2}})
    assert_equal(2, res.content['b']['c'])
    assert_equal('application/json; charset=utf-8', res.content_type)
    # #previous contains the original response
    assert_equal(1, JSON.parse(res.previous.content)['a'])
  end

  def test_post_with_array
    res = @client.post(serverurl + 'json', [{'a' => 1, 'b' => {'c' => 2}}])
    assert_equal(2, res.content[0]['b']['c'])
    assert_equal('application/json; charset=utf-8', res.content_type)
  end

  def test_post_with_header
    res = @client.post(serverurl + 'json', :header => {'X-foo' => 'bar'}, :body => {'a' => 1, 'b' => {'c' => 2}})
    assert_equal(2, res.content['b']['c'])
    assert_equal('application/json; charset=utf-8', res.content_type)
  end

  def test_post_with_array_header
    res = @client.post(serverurl + 'json', :header => [['X-foo', 'bar']], :body => {'a' => 1, 'b' => {'c' => 2}})
    assert_equal(2, res.content['b']['c'])
    assert_equal('application/json; charset=utf-8', res.content_type)
  end

  def test_post_non_json_body
    res = @client.post(serverurl + 'json', 'a=b&c=d')
    assert_equal('a=b&c=d', res.content)
    assert_equal('application/x-www-form-urlencoded', res.content_type)
  end

  def test_put
    res = @client.put(serverurl + 'json', {'a' => 1, 'b' => {'c' => 2}})
    assert_equal(2, res.content['b']['c'])
    assert_equal('application/json; charset=utf-8', res.content_type)
  end

  def test_get_not_affected
    res = @client.get(serverurl + 'json', {'a' => 1, 'b' => {'c' => 2}})
    assert_equal('', res.content)
    assert_equal('', res.content_type)
  end

  def test_hash_header_not_modified
    header = {'X-foo' => 'bar'}
    _res = @client.post(serverurl, :header => header, :body => {'a' => 1, 'b' => {'c' => 2}})
    assert_equal({'X-foo' => 'bar'}, header)
  end

  def test_array_header_not_modified
    header = [['X-foo', 'bar']]
    _res = @client.post(serverurl, :header => header, :body => {'a' => 1, 'b' => {'c' => 2}})
    assert_equal([['X-foo', 'bar']], header)
  end

  class JSONServlet < WEBrick::HTTPServlet::AbstractServlet
    def get_instance(*arg)
      self
    end

    def service(req, res)
      res['content-type'] = req['content-type']
      res.body = req.body
    end
  end

  def setup_server
    @server = WEBrick::HTTPServer.new(
      :BindAddress => "localhost",
      :Logger => @logger,
      :Port => 50000,
      :AccessLog => [],
      :DocumentRoot => File.dirname(File.expand_path(__FILE__))
    )
    @serverport = @server.config[:Port]
    @server.mount('/json', JSONServlet.new(@server))
    @server_thread = start_server_thread(@server)
  end
end