Module: Hurley::Test::Integration::Common

Defined in:
lib/hurley/test/integration.rb

Instance Method Summary collapse

Instance Method Details

#clientObject



195
196
197
198
199
200
201
202
203
204
# File 'lib/hurley/test/integration.rb', line 195

def client
  @client ||= Client.new(Integration.live_endpoint) do |cli|
    cli.header["X-Hurley-Connection"] = connection.class.name
    cli.connection = connection

    if Integration.ssl?
      cli.ssl_options.ca_file = Integration.ssl_file
    end
  end
end

#proxy_urlObject



206
207
208
209
210
# File 'lib/hurley/test/integration.rb', line 206

def proxy_url
  @proxy_url ||= if raw_url = ENV["HURLEY_PROXY"]
    Url.parse(raw_url)
  end
end

#test_connection_errorObject



163
164
165
166
167
# File 'lib/hurley/test/integration.rb', line 163

def test_connection_error
  assert_raises Hurley::ConnectionFailed do
    client.get "http://localhost:4"
  end
end

#test_DELETE_retrieves_the_bodyObject



152
153
154
# File 'lib/hurley/test/integration.rb', line 152

def test_DELETE_retrieves_the_body
  assert_equal %(delete), client.delete("echo").body
end

#test_DELETE_retrieves_the_response_headersObject



148
149
150
# File 'lib/hurley/test/integration.rb', line 148

def test_DELETE_retrieves_the_response_headers
  assert_match(/text\/plain/, client.delete("echo").header[:content_type])
end

#test_empty_body_response_represented_as_nilObject



169
170
171
172
173
# File 'lib/hurley/test/integration.rb', line 169

def test_empty_body_response_represented_as_nil
  res = client.get("204")
  assert_equal 204, res.status_code
  assert_nil res.body
end

#test_GET_retrieves_the_response_bodyObject



30
31
32
# File 'lib/hurley/test/integration.rb', line 30

def test_GET_retrieves_the_response_body
  assert_equal "get", client.get("echo").body
end

#test_GET_retrieves_the_response_headersObject



38
39
40
41
42
43
# File 'lib/hurley/test/integration.rb', line 38

def test_GET_retrieves_the_response_headers
  response = client.get("echo")
  assert_match(/text\/plain/, response.header["Content-Type"])
  assert_match(/text\/plain/, response.header["content-type"])
  assert_match(/text\/plain/, response.header[:content_type])
end

#test_GET_send_url_encoded_paramsObject



34
35
36
# File 'lib/hurley/test/integration.rb', line 34

def test_GET_send_url_encoded_params
  assert_equal %(get ?{"name"=>"zack"}), client.get("echo", :name => :zack).body
end

#test_GET_sends_user_agentObject



45
46
47
# File 'lib/hurley/test/integration.rb', line 45

def test_GET_sends_user_agent
  assert_equal Hurley::USER_AGENT, client.get("echo_header", :name => :user_agent).body
end

#test_GET_sslObject



49
50
51
52
# File 'lib/hurley/test/integration.rb', line 49

def test_GET_ssl
  expected = Integration.ssl?.to_s
  assert_equal expected, client.get("ssl").body
end

#test_HEAD_retrieves_no_response_bodyObject



140
141
142
# File 'lib/hurley/test/integration.rb', line 140

def test_HEAD_retrieves_no_response_body
  assert_nil client.head("echo").body
end

#test_HEAD_retrieves_the_response_headersObject



144
145
146
# File 'lib/hurley/test/integration.rb', line 144

def test_HEAD_retrieves_the_response_headers
  assert_match(/text\/plain/, client.head("echo").header[:content_type])
end

#test_OPTIONSObject



136
137
138
# File 'lib/hurley/test/integration.rb', line 136

def test_OPTIONS
  assert_equal "options", client.options("echo").body
end

#test_PATCH_send_url_encoded_paramsObject



128
129
130
131
132
133
134
# File 'lib/hurley/test/integration.rb', line 128

def test_PATCH_send_url_encoded_params
  res = client.patch "echo" do |req|
    req.header[:content_type] = "application/x-www-form-urlencoded"
    req.body = "name=zack"
  end
  assert_equal %(patch {"name"=>"zack"}), res.body
end

#test_POST_retrieves_the_response_headersObject



70
71
72
# File 'lib/hurley/test/integration.rb', line 70

def test_POST_retrieves_the_response_headers
  assert_match(/text\/plain/, client.post("echo").header[:content_type])
end

#test_POST_send_url_encoded_nested_paramsObject



62
63
64
65
66
67
68
# File 'lib/hurley/test/integration.rb', line 62

def test_POST_send_url_encoded_nested_params
  res = client.post "echo" do |req|
    req.body = "name[first]=zack"
    req.header[:content_type] = "application/x-www-form-urlencoded"
  end
  assert_equal %(post {"name"=>{"first"=>"zack"}}), res.body
end

#test_POST_send_url_encoded_paramsObject



54
55
56
57
58
59
60
# File 'lib/hurley/test/integration.rb', line 54

def test_POST_send_url_encoded_params
  res = client.post "echo" do |req|
    req.body = "name=zack"
    req.header[:content_type] = "application/x-www-form-urlencoded"
  end
  assert_equal %(post {"name"=>"zack"}), res.body
end

#test_POST_sends_files_as_multipartObject



74
75
76
77
78
79
80
81
82
83
# File 'lib/hurley/test/integration.rb', line 74

def test_POST_sends_files_as_multipart
  resp = client.post("file") do |req|
    ctype, body = Query::Flat.new(
      :uploaded_file => UploadIO.new(__FILE__, "text/x-ruby"),
    ).to_form
    req.header[:content_type] = ctype
    req.body = body
  end
  assert_equal "file integration.rb text/x-ruby #{File.size(__FILE__)}", resp.body
end

#test_proxyObject



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/hurley/test/integration.rb', line 175

def test_proxy
  return unless client.request_options.proxy = proxy_url

  res = client.get("/echo")
  assert_equal "get", res.body

  unless Integration.ssl?
    # proxy can't append "Via" header for HTTPS responses
    assert_match(/:#{proxy_url.port}$/, res.header[:Via])
  end
end

#test_proxy_auth_failObject



187
188
189
190
191
192
193
# File 'lib/hurley/test/integration.rb', line 187

def test_proxy_auth_fail
  return unless client.request_options.proxy = proxy_url
  client.request_options.proxy.password = "WRONG"
  assert_equal 407, client.put("/echo").status_code
rescue Hurley::ConnectionFailed
  # this exception is allowed too
end

#test_PUT_retrieves_the_response_headersObject



124
125
126
# File 'lib/hurley/test/integration.rb', line 124

def test_PUT_retrieves_the_response_headers
  assert_match(/text\/plain/, client.put("echo").header[:content_type])
end

#test_PUT_send_url_encoded_nested_paramsObject



116
117
118
119
120
121
122
# File 'lib/hurley/test/integration.rb', line 116

def test_PUT_send_url_encoded_nested_params
  res = client.put "echo" do |req|
    req.body = "name[first]=zack"
    req.header[:content_type] = "application/x-www-form-urlencoded"
  end
  assert_equal %(put {"name"=>{"first"=>"zack"}}), res.body
end

#test_PUT_send_url_encoded_paramsObject



108
109
110
111
112
113
114
# File 'lib/hurley/test/integration.rb', line 108

def test_PUT_send_url_encoded_params
  res = client.put "echo" do |req|
    req.body = "name=zack"
    req.header[:content_type] = "application/x-www-form-urlencoded"
  end
  assert_equal %(put {"name"=>"zack"}), res.body
end

#test_PUT_sends_filesObject



85
86
87
88
89
90
91
# File 'lib/hurley/test/integration.rb', line 85

def test_PUT_sends_files
  resp = client.put("raw") do |req|
    req.header[:content_length] = File.size(__FILE__)
    req.body = File.open(__FILE__)
  end
  assert_equal "raw application/octet-stream #{File.size(__FILE__)} #{File.size(__FILE__)}", resp.body
end

#test_PUT_sends_ioObject



93
94
95
96
97
98
99
# File 'lib/hurley/test/integration.rb', line 93

def test_PUT_sends_io
  resp = client.put("raw") do |req|
    req.body = GenericIO.new("READ")
    req.header[:content_length] = 4
  end
  assert_equal "raw application/octet-stream 4 4", resp.body
end

#test_PUT_sends_io_with_chunked_encodingObject



101
102
103
104
105
106
# File 'lib/hurley/test/integration.rb', line 101

def test_PUT_sends_io_with_chunked_encoding
  resp = client.put("raw") do |req|
    req.body = GenericIO.new("READ")
  end
  assert_equal "raw application/octet-stream -1 4", resp.body
end

#test_timeoutObject



156
157
158
159
160
161
# File 'lib/hurley/test/integration.rb', line 156

def test_timeout
  client.request_options.timeout = 0.5
  assert_raises Hurley::Timeout do
    client.get "/slow"
  end
end