From 88e3a92ffbd607eb1e07067219d4c54652ca6376 Mon Sep 17 00:00:00 2001 From: Mario Ruiz Date: Tue, 22 Jan 2019 12:11:26 +0000 Subject: [PATCH] added tests for redirection --- spec/nice_http/nice_http_delete_spec.rb | 12 +++++++++++ spec/nice_http/nice_http_get_spec.rb | 6 ++++++ spec/nice_http/nice_http_head_spec.rb | 24 ++++++++++++++++++++++ spec/nice_http/nice_http_patch_spec.rb | 27 ++++++++++++++++++++++++- spec/nice_http/nice_http_post_spec.rb | 27 ++++++++++++++++++++++++- spec/nice_http/nice_http_put_spec.rb | 14 ++++++++++++- 6 files changed, 107 insertions(+), 3 deletions(-) diff --git a/spec/nice_http/nice_http_delete_spec.rb b/spec/nice_http/nice_http_delete_spec.rb index a758c48..8a390d5 100644 --- a/spec/nice_http/nice_http_delete_spec.rb +++ b/spec/nice_http/nice_http_delete_spec.rb @@ -42,4 +42,16 @@ expect(resp.data.json).to eq ({ example: "mock" }) end + it 'doesn\'t redirect when auto_redirect is false and http code is 30x' do + server = "http://examplesinatra--tcblues.repl.co/" + http = NiceHttp.new(server) + http.auto_redirect = false + req = { + path: '/exampleRedirect', + data: {example: 'example'}, + } + resp = http.delete(req) + expect(resp.code).to eq 303 + end + end diff --git a/spec/nice_http/nice_http_get_spec.rb b/spec/nice_http/nice_http_get_spec.rb index 3a51679..e9cad50 100644 --- a/spec/nice_http/nice_http_get_spec.rb +++ b/spec/nice_http/nice_http_get_spec.rb @@ -67,6 +67,12 @@ expect(resp.message).to eq 'Found' end + it 'handles correctly when http or https is on path' do + resp = @http.get 'https://www.reqres.in/api/users?page=2' + expect(resp.code).to eq 200 + expect(resp.message).to eq 'OK' + end + it 'set the cookies when required' do server = "https://samples.auth0.com/" path = "/authorize?client_id=kbyuFDidLLm280LIwVFiazOqjO3ty8KH&response_type=code" diff --git a/spec/nice_http/nice_http_head_spec.rb b/spec/nice_http/nice_http_head_spec.rb index 845d41f..1f40907 100644 --- a/spec/nice_http/nice_http_head_spec.rb +++ b/spec/nice_http/nice_http_head_spec.rb @@ -52,5 +52,29 @@ expect(http.cookies['/'].key?("auth0")).to eq true end + it 'redirects when auto_redirect is true and http code is 30x' do + server = "http://examplesinatra--tcblues.repl.co/" + http = NiceHttp.new(server) + http.auto_redirect = true + req = { + path: '/exampleRedirect', + data: {example: 'example'}, + } + resp = http.head(req) + expect(resp.code).to eq 200 + expect(resp.message).to eq 'OK' + end + + it 'doesn\'t redirect when auto_redirect is false and http code is 30x' do + server = "http://examplesinatra--tcblues.repl.co/" + http = NiceHttp.new(server) + http.auto_redirect = false + req = { + path: '/exampleRedirect', + data: {example: 'example'}, + } + resp = http.head(req) + expect(resp.code).to eq 303 + end end diff --git a/spec/nice_http/nice_http_patch_spec.rb b/spec/nice_http/nice_http_patch_spec.rb index 5f1b8c7..3fe0748 100644 --- a/spec/nice_http/nice_http_patch_spec.rb +++ b/spec/nice_http/nice_http_patch_spec.rb @@ -68,6 +68,31 @@ expect(resp.data.json(:name)).to eq 'peter' end - #todo: add tests for redirection, headers, encoding and cookies + it 'redirects when auto_redirect is true and http code is 30x' do + server = "http://examplesinatra--tcblues.repl.co/" + http = NiceHttp.new(server) + http.auto_redirect = true + req = { + path: '/exampleRedirect', + data: {example: 'example'}, + } + resp = http.patch(req) + expect(resp.code).to eq 200 + expect(resp.message).to eq 'OK' + end + + it 'doesn\'t redirect when auto_redirect is false and http code is 30x' do + server = "http://examplesinatra--tcblues.repl.co/" + http = NiceHttp.new(server) + http.auto_redirect = false + req = { + path: '/exampleRedirect', + data: {example: 'example'}, + } + resp = http.patch(req) + expect(resp.code).to eq 303 + end + + #todo: add tests for headers, encoding and cookies end diff --git a/spec/nice_http/nice_http_post_spec.rb b/spec/nice_http/nice_http_post_spec.rb index 6524c7c..4cca5fc 100644 --- a/spec/nice_http/nice_http_post_spec.rb +++ b/spec/nice_http/nice_http_post_spec.rb @@ -67,6 +67,31 @@ expect(resp.data.json(:name)).to eq 'peter' end - #todo: add tests for redirection, headers, encoding and cookies + it 'redirects when auto_redirect is true and http code is 30x' do + server = "http://examplesinatra--tcblues.repl.co/" + http = NiceHttp.new(server) + http.auto_redirect = true + req = { + path: '/exampleRedirect', + data: {example: 'example'}, + } + resp = http.post(req) + expect(resp.code).to eq 200 + expect(resp.message).to eq 'OK' + end + + it 'doesn\'t redirect when auto_redirect is false and http code is 30x' do + server = "http://examplesinatra--tcblues.repl.co/" + http = NiceHttp.new(server) + http.auto_redirect = false + req = { + path: '/exampleRedirect', + data: {example: 'example'}, + } + resp = http.post(req) + expect(resp.code).to eq 303 + end + + #todo: add tests headers, encoding and cookies end diff --git a/spec/nice_http/nice_http_put_spec.rb b/spec/nice_http/nice_http_put_spec.rb index a439d92..6decb83 100644 --- a/spec/nice_http/nice_http_put_spec.rb +++ b/spec/nice_http/nice_http_put_spec.rb @@ -68,6 +68,18 @@ expect(resp.data.json(:name)).to eq 'peter' end - #todo: add tests for redirection, headers, encoding and cookies + it 'doesn\'t redirect when auto_redirect is false and http code is 30x' do + server = "http://examplesinatra--tcblues.repl.co/" + http = NiceHttp.new(server) + http.auto_redirect = false + req = { + path: '/exampleRedirect', + data: {example: 'example'}, + } + resp = http.put(req) + expect(resp.code).to eq 303 + end + + #todo: add tests for headers, encoding and cookies end