Class: RequestMiddlewareTest

Inherits:
Faraday::TestCase show all
Defined in:
test/request_middleware_test.rb

Instance Method Summary collapse

Methods inherited from Faraday::TestCase

#capture_warnings, jruby?, rbx?, ssl_mode?, #test_default

Methods included from Faraday::LiveServerConfig

#live_server, #live_server=, #live_server?

Instance Method Details

#setupObject


7
8
9
10
11
12
13
14
15
16
17
18
# File 'test/request_middleware_test.rb', line 7

def setup
  @conn = Faraday.new do |b|
    b.request :multipart
    b.request :url_encoded
    b.adapter :test do |stub|
      stub.post('/echo') do |env|
        posted_as = env[:request_headers]['Content-Type']
        [200, {'Content-Type' => posted_as}, env[:body]]
      end
    end
  end
end

#test_does_nothing_without_payloadObject


34
35
36
37
38
# File 'test/request_middleware_test.rb', line 34

def test_does_nothing_without_payload
  response = @conn.post('/echo')
  assert_nil response.headers['Content-Type']
  assert response.body.empty?
end

#test_ignores_custom_content_typeObject


40
41
42
43
44
# File 'test/request_middleware_test.rb', line 40

def test_ignores_custom_content_type
  response = @conn.post('/echo', { :some => 'data' }, 'content-type' => 'application/x-foo')
  assert_equal 'application/x-foo', response.headers['Content-Type']
  assert_equal({ :some => 'data' }, response.body)
end

#test_multipartObject


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'test/request_middleware_test.rb', line 98

def test_multipart  # assume params are out of order

  regexes = [
    /name\=\"a\"/,
    /name=\"b\[c\]\"\; filename\=\"request_middleware_test\.rb\"/,
    /name=\"b\[d\]\"/]

  payload = {:a => 1, :b => {:c => Faraday::UploadIO.new(__FILE__, 'text/x-ruby'), :d => 2}}
  response = @conn.post('/echo', payload)

  assert_kind_of Faraday::CompositeReadIO, response.body
  assert_equal "multipart/form-data; boundary=%s" % Faraday::Request::Multipart::DEFAULT_BOUNDARY,
    response.headers['Content-Type']

  response.body.send(:ios).map{|io| io.read}.each do |io|
    if re = regexes.detect { |r| io =~ r }
      regexes.delete re
    end
  end
  assert_equal [], regexes
end

#test_multipart_with_arraysObject


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'test/request_middleware_test.rb', line 120

def test_multipart_with_arrays  # assume params are out of order

  regexes = [
    /name\=\"a\"/,
    /name=\"b\[\]\[c\]\"\; filename\=\"request_middleware_test\.rb\"/,
    /name=\"b\[\]\[d\]\"/]

  payload = {:a => 1, :b =>[{:c => Faraday::UploadIO.new(__FILE__, 'text/x-ruby'), :d => 2}]}
  response = @conn.post('/echo', payload)

  assert_kind_of Faraday::CompositeReadIO, response.body
  assert_equal "multipart/form-data; boundary=%s" % Faraday::Request::Multipart::DEFAULT_BOUNDARY,
    response.headers['Content-Type']

  response.body.send(:ios).map{|io| io.read}.each do |io|
    if re = regexes.detect { |r| io =~ r }
      regexes.delete re
    end
  end
  assert_equal [], regexes
end

#test_url_encoded_nestedObject


58
59
60
61
62
63
# File 'test/request_middleware_test.rb', line 58

def test_url_encoded_nested
  response = @conn.post('/echo', { :user => {:name => 'Mislav', :web => 'mislav.net'} })
  assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
  expected = { 'user' => {'name' => 'Mislav', 'web' => 'mislav.net'} }
  assert_equal expected, Faraday::Utils.parse_nested_query(response.body)
end

#test_url_encoded_nested_keysObject


93
94
95
96
# File 'test/request_middleware_test.rb', line 93

def test_url_encoded_nested_keys
  response = @conn.post('/echo', {'a'=>{'b'=>{'c'=>['d']}}})
  assert_equal "a%5Bb%5D%5Bc%5D%5B%5D=d", response.body
end

#test_url_encoded_no_headerObject


46
47
48
49
50
# File 'test/request_middleware_test.rb', line 46

def test_url_encoded_no_header
  response = @conn.post('/echo', { :fruit => %w[apples oranges] })
  assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
  assert_equal 'fruit%5B%5D=apples&fruit%5B%5D=oranges', response.body
end

#test_url_encoded_non_nestedObject


65
66
67
68
69
70
71
72
73
# File 'test/request_middleware_test.rb', line 65

def test_url_encoded_non_nested
  response = @conn.post('/echo', { :dimensions => ['date', 'location']}) do |req|
    req.options.params_encoder = Faraday::FlatParamsEncoder
  end
  assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
  expected = { 'dimensions' => ['date', 'location'] }
  assert_equal expected, Faraday::Utils.parse_query(response.body)
  assert_equal 'dimensions=date&dimensions=location', response.body
end

#test_url_encoded_unicodeObject


75
76
77
78
79
80
81
# File 'test/request_middleware_test.rb', line 75

def test_url_encoded_unicode
  err = capture_warnings {
    response = @conn.post('/echo', {:str => "eé cç aã aâ"})
    assert_equal "str=e%C3%A9+c%C3%A7+a%C3%A3+a%C3%A2", response.body
  }
  assert err.empty?, "stderr did include: #{err}"
end

#test_url_encoded_unicode_with_kcode_setObject


83
84
85
86
87
88
89
90
91
# File 'test/request_middleware_test.rb', line 83

def test_url_encoded_unicode_with_kcode_set
  with_utf8 do
    err = capture_warnings {
      response = @conn.post('/echo', {:str => "eé cç aã aâ"})
      assert_equal "str=e%C3%A9+c%C3%A7+a%C3%A3+a%C3%A2", response.body
    }
    assert err.empty?, "stderr did include: #{err}"
  end
end

#test_url_encoded_with_headerObject


52
53
54
55
56
# File 'test/request_middleware_test.rb', line 52

def test_url_encoded_with_header
  response = @conn.post('/echo', {'a'=>123}, 'content-type' => 'application/x-www-form-urlencoded')
  assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
  assert_equal 'a=123', response.body
end

#with_utf8Object


20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'test/request_middleware_test.rb', line 20

def with_utf8
  if defined?(RUBY_VERSION) && RUBY_VERSION.match(/1.8.\d/)
    begin
      previous_kcode = $KCODE
      $KCODE = "UTF8"
      yield
    ensure
      $KCODE = previous_kcode
    end
  else
    yield
  end
end