|
| 1 | +require"spec_helper" |
| 2 | + |
| 3 | +RSpec.describeZencoder::HTTP::NetHTTPdo |
| 4 | + |
| 5 | +describe"call options"do |
| 6 | +it"requests with timeout"do |
| 7 | +stub_request(:post,"https://example.com") |
| 8 | +expect(Timeout).toreceive(:timeout).with(0.001) |
| 9 | +Zencoder::HTTP::NetHTTP.post('https://example.com',:timeout=>1) |
| 10 | +end |
| 11 | + |
| 12 | +it"requests without timeout"do |
| 13 | +stub_request(:post,"https://example.com") |
| 14 | +allow(Timeout).toreceive(:timeout).and_raise(Timeout::Error) |
| 15 | +assert_nothing_raiseddo |
| 16 | +Zencoder::HTTP::NetHTTP.post('https://example.com',:timeout=>nil) |
| 17 | +end |
| 18 | +end |
| 19 | + |
| 20 | +it"adds params to the query string if passed"do |
| 21 | +stub_request(:post,"https://example.com/path?some=param") |
| 22 | +Zencoder::HTTP::NetHTTP.post('https://example.com/path',{:params=>{:some=>'param'}}) |
| 23 | +end |
| 24 | + |
| 25 | +it"adds params to the existing query string if passed"do |
| 26 | +stub_request(:post,'https://example.com/path?original=param&some=param') |
| 27 | +Zencoder::HTTP::NetHTTP.post('https://example.com/path?original=param',{:params=>{:some=>'param'}}) |
| 28 | +end |
| 29 | + |
| 30 | +it"adds headers"do |
| 31 | +stub_request(:post,'https://example.com/path').with(:headers=>{'some'=>'header'}) |
| 32 | +Zencoder::HTTP::NetHTTP.post('https://example.com/path',{:headers=>{:some=>'header'}}) |
| 33 | +end |
| 34 | + |
| 35 | +it"adds the body to the request"do |
| 36 | +stub_request(:post,'https://example.com/path').with(:body=>'{"some": "body"}') |
| 37 | +Zencoder::HTTP::NetHTTP.post('https://example.com/path',{:body=>'{"some": "body"}'}) |
| 38 | +end |
| 39 | +end |
| 40 | + |
| 41 | +describe"SSL verification"do |
| 42 | +beforedo |
| 43 | +@cert_store=double(:add_file=>true,:add_path=>true,:flags==>true,:set_default_paths=>true).as_null_object |
| 44 | +@http_stub=double(:use_ssl==>true,:request=>true,:verify_mode==>true,:cert_store==>true,:cert_store=>@cert_store).as_null_object |
| 45 | +expect(::Net::HTTP).toreceive(:new).and_return(@http_stub) |
| 46 | +end |
| 47 | + |
| 48 | +describe"when set to skip ssl verification"do |
| 49 | +it"nots verify"do |
| 50 | +expect(@http_stub).toreceive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE) |
| 51 | +Zencoder::HTTP::NetHTTP.post('https://example.com/path',:skip_ssl_verify=>true) |
| 52 | +end |
| 53 | + |
| 54 | +it"nots setup a custom cert store"do |
| 55 | +expect(@http_stub).to_notreceive(:cert_store=) |
| 56 | +Zencoder::HTTP::NetHTTP.post('https://example.com/path',:skip_ssl_verify=>true) |
| 57 | +end |
| 58 | +end |
| 59 | + |
| 60 | +describe"when set to do ssl verification"do |
| 61 | +it"setups a custom cert store"do |
| 62 | +expect(@http_stub).toreceive(:cert_store=) |
| 63 | +Zencoder::HTTP::NetHTTP.post('https://example.com/path') |
| 64 | +end |
| 65 | + |
| 66 | +it"sets the default paths on the custom cert store"do |
| 67 | +expect(@cert_store).toreceive(:set_default_paths) |
| 68 | +Zencoder::HTTP::NetHTTP.post('https://example.com/path') |
| 69 | +end |
| 70 | + |
| 71 | +it"sets the ca_file when it is passed in"do |
| 72 | +expect(@cert_store).toreceive(:add_file).with("/foo/bar/baz.crt") |
| 73 | +Zencoder::HTTP::NetHTTP.post('https://example.com/path',:ca_file=>"/foo/bar/baz.crt") |
| 74 | +end |
| 75 | + |
| 76 | +it"sets the ca_path when it is passed in"do |
| 77 | +expect(@cert_store).toreceive(:add_path).with("/foo/bar/") |
| 78 | +Zencoder::HTTP::NetHTTP.post('https://example.com/path',:ca_path=>"/foo/bar/") |
| 79 | +end |
| 80 | +end |
| 81 | + |
| 82 | +end |
| 83 | + |
| 84 | +describe".post"do |
| 85 | +it"POSTs to specified body to the specified path"do |
| 86 | +stub_request(:post,'https://example.com').with(:body=>'{}') |
| 87 | +Zencoder::HTTP::NetHTTP.post('https://example.com',:body=>'{}') |
| 88 | +end |
| 89 | + |
| 90 | +it"POSTs with an empty body if none is provided"do |
| 91 | +stub_request(:post,'https://example.com').with(:body=>'') |
| 92 | +Zencoder::HTTP::NetHTTP.post('https://example.com') |
| 93 | +end |
| 94 | +end |
| 95 | + |
| 96 | +describe".put"do |
| 97 | +it"PUTs to specified body to the specified path"do |
| 98 | +stub_request(:put,'https://example.com').with(:body=>'{}') |
| 99 | +Zencoder::HTTP::NetHTTP.put('https://example.com',:body=>'{}') |
| 100 | +end |
| 101 | + |
| 102 | +it"PUTs with an empty body if none is provided"do |
| 103 | +stub_request(:put,'https://example.com').with(:body=>'') |
| 104 | +Zencoder::HTTP::NetHTTP.put('https://example.com') |
| 105 | +end |
| 106 | +end |
| 107 | + |
| 108 | +describe".get"do |
| 109 | +it"GETs to specified body to the specified path"do |
| 110 | +stub_request(:get,'https://example.com') |
| 111 | +Zencoder::HTTP::NetHTTP.get('https://example.com') |
| 112 | +end |
| 113 | +end |
| 114 | + |
| 115 | +describe".delete"do |
| 116 | +it"DELETEs to specified body to the specified path"do |
| 117 | +stub_request(:delete,'https://example.com') |
| 118 | +Zencoder::HTTP::NetHTTP.delete('https://example.com') |
| 119 | +end |
| 120 | +end |
| 121 | +end |