nice_http 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +331 -325
- data/lib/nice_http.rb +1188 -1195
- data/lib/nice_http/utils.rb +109 -109
- metadata +32 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a5130a17c74e266e3867624db6c52cb602ce96af5c8abc318b4e90fd90769d0
|
4
|
+
data.tar.gz: a99c7f2aa2e61c2eb5436d28ac7c6fa5e01087a85cd62c984bf53cfce33d9864
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa7f863a14d48983d188265d44f01c05a18206ac8a656c61ed0c18bc9bad1280dbdc1bb7999c078fbe490811f3192c03c96387f81c8c5872f6dc0c89ce6b563e
|
7
|
+
data.tar.gz: '05739e8ecfa8f834df126c10b179420a461b2791b96e0e0e03ae3a9924b72c76c9f930eed83c49b2d43ac33c8d2800f9b02f8566a3defa2a171b0802d949e484'
|
data/README.md
CHANGED
@@ -1,325 +1,331 @@
|
|
1
|
-
# NiceHttp
|
2
|
-
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/nice_http.svg)](https://rubygems.org/gems/nice_http)
|
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
|
-
resp
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
# as
|
58
|
-
|
59
|
-
|
60
|
-
# as
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
NiceHttp.
|
76
|
-
NiceHttp.
|
77
|
-
NiceHttp.
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
resp
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
p "
|
268
|
-
p "
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
p "
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
"
|
290
|
-
"
|
291
|
-
"
|
292
|
-
"
|
293
|
-
|
294
|
-
"
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
1
|
+
# NiceHttp
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/nice_http.svg)](https://rubygems.org/gems/nice_http)
|
4
|
+
[![Build Status](https://travis-ci.com/MarioRuiz/nice_http.svg?branch=master)](https://github.com/MarioRuiz/nice_http)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/github/MarioRuiz/nice_http/badge.svg?branch=master)](https://coveralls.io/github/MarioRuiz/nice_http?branch=master)
|
6
|
+
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FMarioRuiz%2Fnice_http.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2FMarioRuiz%2Fnice_http?ref=badge_shield)
|
7
|
+
|
8
|
+
NiceHttp the simplest library for accessing and testing HTTP and REST resources.
|
9
|
+
|
10
|
+
Manage different hosts on the fly. Easily get the value you want from the JSON strings. Use hashes on your requests.
|
11
|
+
|
12
|
+
Also you can use mock responses by using :mock_response key on the request hash and enable the use_mocks option on NiceHttp.
|
13
|
+
|
14
|
+
NiceHttp will take care of the redirections and the cookies, and for security tests you will be able to modify the cookies or disable and control the redirections by yourself.
|
15
|
+
|
16
|
+
To be able to generate random requests take a look at the documentation for nice_hash gem: https://github.com/MarioRuiz/nice_hash
|
17
|
+
|
18
|
+
Example that creates 1000 good random and unique requests to register an user and test that the validation of the fields are correct by the user was able to be registered. Send 800 requests where just one field is wrong and verify the user was not able to be created: https://gist.github.com/MarioRuiz/824d7a462b62fd85f02c1a09455deefb
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Install it yourself as:
|
23
|
+
|
24
|
+
$ gem install nice_http
|
25
|
+
|
26
|
+
|
27
|
+
## A very simple first example
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'nice_http'
|
31
|
+
|
32
|
+
http = NiceHttp.new('https://reqres.in')
|
33
|
+
|
34
|
+
resp = http.get("/api/users?page=2")
|
35
|
+
|
36
|
+
pp resp.code
|
37
|
+
pp resp.data.json
|
38
|
+
|
39
|
+
resp = http.get("/api/users/2")
|
40
|
+
|
41
|
+
pp resp.data.json(:first_name, :last_name)
|
42
|
+
|
43
|
+
resp = http.post( {
|
44
|
+
path: "/api/users",
|
45
|
+
data: { name: "morpheus", job: "leader" }
|
46
|
+
} )
|
47
|
+
|
48
|
+
pp resp.data.json
|
49
|
+
```
|
50
|
+
|
51
|
+
## Create a connection
|
52
|
+
|
53
|
+
The simplest way is just by supplying the value as an argument:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
|
57
|
+
# as an url
|
58
|
+
http1 = NiceHttp.new("https://example.com")
|
59
|
+
|
60
|
+
# as parameters
|
61
|
+
http2 = NiceHttp.new( host: "reqres.in", port: 443, ssl: true )
|
62
|
+
|
63
|
+
# as a hash
|
64
|
+
http3 = NiceHttp.new my_reqres_server
|
65
|
+
|
66
|
+
|
67
|
+
```
|
68
|
+
|
69
|
+
|
70
|
+
You can specify all the defaults you will be using when creating connections by using the NiceHttp methods, in this example, http1 and http2 will be connecting to reqres.in with the default parameters and http3 to example.com:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
|
74
|
+
# default parameters
|
75
|
+
NiceHttp.host = 'reqres.in'
|
76
|
+
NiceHttp.ssl = true
|
77
|
+
NiceHttp.port = 443
|
78
|
+
NiceHttp.debug = false
|
79
|
+
NiceHttp.log = "./my_logs.log"
|
80
|
+
NiceHttp.headers = {"api-key": "the api key"}
|
81
|
+
|
82
|
+
http1 = NiceHttp.new()
|
83
|
+
|
84
|
+
http2 = NiceHttp.new()
|
85
|
+
|
86
|
+
http3 = NiceHttp.new("https://example.com")
|
87
|
+
|
88
|
+
```
|
89
|
+
|
90
|
+
If you prefer to supply a hash to change the default settings for NiceHttp:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
NiceHttp.defaults = {
|
94
|
+
host: 'reqres.in',
|
95
|
+
ssl: true,
|
96
|
+
port: 443,
|
97
|
+
debug: false,
|
98
|
+
log: "./my_logs.log",
|
99
|
+
headers: {"api-key": "the api key"}
|
100
|
+
}
|
101
|
+
```
|
102
|
+
|
103
|
+
|
104
|
+
## Creating requests
|
105
|
+
|
106
|
+
You can use hash requests to simplify the management of your requests, for example creating a file specifying all the requests for your Customers API.
|
107
|
+
|
108
|
+
The keys you can use:
|
109
|
+
|
110
|
+
*path*: relative or absolute path, for example: "/api2/customers/update.do"
|
111
|
+
|
112
|
+
*headers*: specific headers for the request. It will include a hash with the values.
|
113
|
+
|
114
|
+
*data*: the data to be sent for example a JSON string. In case of supplying a Hash, Nice Http will assume that is a JSON and will convert it to a JSON string before sending the request and will add to the headers: 'Content-Type': 'application/json'
|
115
|
+
|
116
|
+
*mock_response*: In case of use_mocks=true then NiceHttp will return this response
|
117
|
+
|
118
|
+
|
119
|
+
Let's guess you have a file with this data for your requests on */requests/example.rb*:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
|
123
|
+
module Requests
|
124
|
+
|
125
|
+
module Example
|
126
|
+
|
127
|
+
# simple get request example
|
128
|
+
def self.list_of_users()
|
129
|
+
{
|
130
|
+
path: "/api/users?page=2"
|
131
|
+
}
|
132
|
+
end
|
133
|
+
|
134
|
+
# post request example using a request hash that will be converted automatically to a json string
|
135
|
+
def self.create_user_hash()
|
136
|
+
{
|
137
|
+
path: "/api/users",
|
138
|
+
data: {
|
139
|
+
name: "morpheus",
|
140
|
+
job: "leader"
|
141
|
+
}
|
142
|
+
}
|
143
|
+
end
|
144
|
+
|
145
|
+
# post request example using a JSON string
|
146
|
+
def self.create_user_raw()
|
147
|
+
{
|
148
|
+
path: "/api/users",
|
149
|
+
headers: {"Content-Type": "application/json"},
|
150
|
+
data: '{"name": "morpheus","job": "leader"}'
|
151
|
+
}
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
|
156
|
+
end
|
157
|
+
|
158
|
+
```
|
159
|
+
|
160
|
+
|
161
|
+
Then in your code you can require this request file and use it like this:
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
|
165
|
+
resp = http.get Requests::Example.list_of_users
|
166
|
+
|
167
|
+
pp resp.code
|
168
|
+
|
169
|
+
resp = http.post Requests::Example.create_user_hash
|
170
|
+
|
171
|
+
pp resp.data.json
|
172
|
+
|
173
|
+
|
174
|
+
resp = http.post Requests::Example.create_user_raw
|
175
|
+
|
176
|
+
pp resp.data.json(:job)
|
177
|
+
|
178
|
+
|
179
|
+
```
|
180
|
+
|
181
|
+
|
182
|
+
In case you want to modify the request before sending it, for example just changing one field but the rest will be the same, you can supply a new key :values_for in the request hash that will contain a hash with the keys to be changed and NiceHttp will perform the necessary changes at any level:
|
183
|
+
|
184
|
+
```ruby
|
185
|
+
|
186
|
+
req = Requests::Example.create_user_hash
|
187
|
+
req.values_for = {job: "developer"}
|
188
|
+
|
189
|
+
resp = http.post req
|
190
|
+
|
191
|
+
pp resp.data.json
|
192
|
+
#response: {:name=>"morpheus", :job=>"developer", :id=>"192", :createdAt=>"2018-12-14T14:41:54.371Z"}
|
193
|
+
|
194
|
+
```
|
195
|
+
|
196
|
+
## Responses
|
197
|
+
|
198
|
+
The response will include at least the keys:
|
199
|
+
|
200
|
+
*code*: the http code response, for example: 200
|
201
|
+
|
202
|
+
*message*: the http message response, for example: "OK"
|
203
|
+
|
204
|
+
*data*: the data response structure. In case of json we can get it as a hash by using: `resp.data.json`. Also you can filter the json structure and get what you want: `resp.data.json(:loginname, :address)`
|
205
|
+
|
206
|
+
Also interesting keys would be: *time_elapsed_total*, *time_elapsed* and many more available
|
207
|
+
|
208
|
+
|
209
|
+
## Special settings
|
210
|
+
|
211
|
+
*debug*: (true or false) it will set the connecition on debug mode so you will be able to see the whole communication with the server in detail
|
212
|
+
|
213
|
+
*log*: (:no, :screen, :file, :fix_file, "filename") it will log the basic communication for inspect. In case you want to add extra info to your logs you can do it for example adding to your code: http.logger.info "example extra log"
|
214
|
+
|
215
|
+
*headers*: Hash containing the headers for the communication
|
216
|
+
|
217
|
+
*cookies*: Hash containing the cookies for the communication
|
218
|
+
|
219
|
+
*proxy_port, proxy_host*: in case you want to use a proxy for the connection
|
220
|
+
|
221
|
+
*use_mocks*: (true or false) in case of true if the request hash contains a mock_response key it will be returning that response instead of trying to send the request.
|
222
|
+
|
223
|
+
*auto_redirect*: (true or false) in case of true it will take care of the auto redirections.
|
224
|
+
|
225
|
+
## Authentication requests
|
226
|
+
|
227
|
+
All we need to do is to add to our request the correct authentication tokens, seeds, headers.
|
228
|
+
|
229
|
+
For example for Basic Authentication we need to add to the authorization header a seed generated with the user and password we want ot authenticate
|
230
|
+
|
231
|
+
```ruby
|
232
|
+
|
233
|
+
@http = NiceHttp.new("https://jigsaw.w3.org/")
|
234
|
+
|
235
|
+
@http.headers.authorization = NiceHttpUtils.basic_authentication(user: "guest", password: "guest")
|
236
|
+
|
237
|
+
# headers will be in this example: {:authorization=>"Basic Z3Vlc3Q6Z3Vlc3Q=\n"}
|
238
|
+
|
239
|
+
resp = @http.get("/HTTP/Basic/")
|
240
|
+
|
241
|
+
```
|
242
|
+
|
243
|
+
Remember for other kind of authentication systems NiceHttp take care of the redirections and cookies that are requested to be set. In case you need to add a header with a token you can add it by using your NiceHttp object and the key headers, for example:
|
244
|
+
|
245
|
+
```ruby
|
246
|
+
@http.headers.tokenuno = "xxx"
|
247
|
+
# headers => {tokenuno: "xxx"}
|
248
|
+
|
249
|
+
#another way:
|
250
|
+
@http.headers[:tokendos] = "yyy"
|
251
|
+
# headers => {tokenuno: "xxx", tokendos: "yyyy"}
|
252
|
+
|
253
|
+
```
|
254
|
+
|
255
|
+
In case you want or need to control the redirections by yourself instead of allowing NiceHttp to do it, then set ```@http.auto_redirect = false```
|
256
|
+
|
257
|
+
An example using OpenID authentication:
|
258
|
+
|
259
|
+
```ruby
|
260
|
+
server = "https://samples.auth0.com/"
|
261
|
+
path="/authorize?client_id=kbyuFDidLLm280LIwVFiazOqjO3ty8KH&response_type=code"
|
262
|
+
|
263
|
+
@http = NiceHttp.new(server)
|
264
|
+
|
265
|
+
resp = @http.get(path)
|
266
|
+
|
267
|
+
p "With autoredirection:"
|
268
|
+
p "Cookies: "
|
269
|
+
p @http.cookies
|
270
|
+
p "Code: #{resp.code} #{resp.message} "
|
271
|
+
p "*"*40
|
272
|
+
|
273
|
+
@http2 = NiceHttp.new(server)
|
274
|
+
@http2.auto_redirect = false
|
275
|
+
|
276
|
+
resp = @http2.get(path)
|
277
|
+
|
278
|
+
p "Without autoredirection:"
|
279
|
+
p "Cookies: "
|
280
|
+
p @http2.cookies
|
281
|
+
p "Code: #{resp.code} #{resp.message} "
|
282
|
+
|
283
|
+
```
|
284
|
+
|
285
|
+
The output:
|
286
|
+
|
287
|
+
```
|
288
|
+
"With autoredirection:"
|
289
|
+
"Cookies: "
|
290
|
+
{"/"=>{"auth0"=>"s%3A6vEEwmmIf-9YAG-NjvsOIyZAh-NS97jj.yFSXILdmCov6DRwXjEei3q3eHIrxZxHI4eg4%2BTpUaK4"},
|
291
|
+
"/usernamepassword/login"=>{"_csrf"=>"bboZ0koMScwXkISzWaAMTYdY"}}
|
292
|
+
"Code: 200 OK "
|
293
|
+
"****************************************"
|
294
|
+
"Without autoredirection:"
|
295
|
+
"Cookies: "
|
296
|
+
{"/"=>{"auth0"=>"s%3AcKndc44gllWyJv8FLztUIctuH4b__g0V.QEF3SOobK8%2FvX89iUKzGbfSP4Vt2bRtY2WH7ygBUkg4"}}
|
297
|
+
"Code: 302 Found "
|
298
|
+
|
299
|
+
```
|
300
|
+
|
301
|
+
|
302
|
+
## Send multipart content
|
303
|
+
|
304
|
+
Example posting a csv file:
|
305
|
+
|
306
|
+
```ruby
|
307
|
+
|
308
|
+
require 'net/http/post/multipart'
|
309
|
+
request = {
|
310
|
+
path: "/customer/profile/",
|
311
|
+
headers: {'Content-Type' => 'multipart/form-data'},
|
312
|
+
data: (Net::HTTP::Post::Multipart.new "/customer/profile/",
|
313
|
+
"file" => UploadIO.new("./path/to/my/file.csv", "text/csv"))
|
314
|
+
}
|
315
|
+
response=@http.post(request)
|
316
|
+
|
317
|
+
```
|
318
|
+
|
319
|
+
## Contributing
|
320
|
+
|
321
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/nice_http.
|
322
|
+
|
323
|
+
|
324
|
+
## License
|
325
|
+
|
326
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
327
|
+
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FMarioRuiz%2Fnice_http.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FMarioRuiz%2Fnice_http?ref=badge_large)
|