David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 20 | require 'spec_helper' |
Kevin Clark | dd33025 | 2008-06-18 01:11:18 +0000 | [diff] [blame] | 21 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 22 | describe 'Thrift::HTTPClientTransport' do |
Kevin Clark | dd33025 | 2008-06-18 01:11:18 +0000 | [diff] [blame] | 23 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 24 | describe Thrift::HTTPClientTransport do |
Kevin Clark | dd33025 | 2008-06-18 01:11:18 +0000 | [diff] [blame] | 25 | before(:each) do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 26 | @client = Thrift::HTTPClientTransport.new("http://my.domain.com/path/to/service?param=value") |
Kevin Clark | dd33025 | 2008-06-18 01:11:18 +0000 | [diff] [blame] | 27 | end |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 28 | |
| 29 | it "should provide a reasonable to_s" do |
| 30 | @client.to_s == "http://my.domain.com/path/to/service?param=value" |
| 31 | end |
Kevin Clark | dd33025 | 2008-06-18 01:11:18 +0000 | [diff] [blame] | 32 | |
| 33 | it "should always be open" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 34 | expect(@client).to be_open |
Kevin Clark | dd33025 | 2008-06-18 01:11:18 +0000 | [diff] [blame] | 35 | @client.close |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 36 | expect(@client).to be_open |
Kevin Clark | dd33025 | 2008-06-18 01:11:18 +0000 | [diff] [blame] | 37 | end |
| 38 | |
| 39 | it "should post via HTTP and return the results" do |
| 40 | @client.write "a test" |
| 41 | @client.write " frame" |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 42 | expect(Net::HTTP).to receive(:new).with("my.domain.com", 80) do |
| 43 | double("Net::HTTP").tap do |http| |
| 44 | expect(http).to receive(:use_ssl=).with(false) |
| 45 | expect(http).to receive(:post).with("/path/to/service?param=value", "a test frame", {"Content-Type"=>"application/x-thrift"}) do |
| 46 | double("Net::HTTPOK").tap do |response| |
| 47 | expect(response).to receive(:body).and_return "data" |
Grégoire Seux | 8ae80a7 | 2019-11-07 11:33:58 +0100 | [diff] [blame] | 48 | expect(response).to receive(:code).and_return "200" |
Roger Meier | a30930f | 2012-05-11 18:08:58 +0000 | [diff] [blame] | 49 | end |
| 50 | end |
Kevin Clark | dd33025 | 2008-06-18 01:11:18 +0000 | [diff] [blame] | 51 | end |
| 52 | end |
| 53 | @client.flush |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 54 | expect(@client.read(10)).to eq("data") |
Kevin Clark | dd33025 | 2008-06-18 01:11:18 +0000 | [diff] [blame] | 55 | end |
Bryan Duxbury | ad776c1 | 2010-08-05 22:12:01 +0000 | [diff] [blame] | 56 | |
| 57 | it "should send custom headers if defined" do |
| 58 | @client.write "test" |
| 59 | custom_headers = {"Cookie" => "Foo"} |
| 60 | headers = {"Content-Type"=>"application/x-thrift"}.merge(custom_headers) |
| 61 | |
| 62 | @client.add_headers(custom_headers) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 63 | expect(Net::HTTP).to receive(:new).with("my.domain.com", 80) do |
| 64 | double("Net::HTTP").tap do |http| |
| 65 | expect(http).to receive(:use_ssl=).with(false) |
| 66 | expect(http).to receive(:post).with("/path/to/service?param=value", "test", headers) do |
| 67 | double("Net::HTTPOK").tap do |response| |
| 68 | expect(response).to receive(:body).and_return "data" |
Grégoire Seux | 8ae80a7 | 2019-11-07 11:33:58 +0100 | [diff] [blame] | 69 | expect(response).to receive(:code).and_return "200" |
Roger Meier | a30930f | 2012-05-11 18:08:58 +0000 | [diff] [blame] | 70 | end |
| 71 | end |
Bryan Duxbury | ad776c1 | 2010-08-05 22:12:01 +0000 | [diff] [blame] | 72 | end |
| 73 | end |
| 74 | @client.flush |
| 75 | end |
John Thomas | eacbd65 | 2016-07-12 08:06:19 -0700 | [diff] [blame] | 76 | |
| 77 | it 'should reset the outbuf on HTTP failures' do |
| 78 | @client.write "test" |
| 79 | |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 80 | expect(Net::HTTP).to receive(:new).with("my.domain.com", 80) do |
| 81 | double("Net::HTTP").tap do |http| |
| 82 | expect(http).to receive(:use_ssl=).with(false) |
| 83 | expect(http).to receive(:post).with("/path/to/service?param=value", "test", {"Content-Type"=>"application/x-thrift"}) { raise Net::ReadTimeout } |
John Thomas | eacbd65 | 2016-07-12 08:06:19 -0700 | [diff] [blame] | 84 | end |
| 85 | end |
| 86 | |
| 87 | @client.flush rescue |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 88 | expect(@client.instance_variable_get(:@outbuf)).to eq(Thrift::Bytes.empty_byte_buffer) |
John Thomas | eacbd65 | 2016-07-12 08:06:19 -0700 | [diff] [blame] | 89 | end |
| 90 | |
Grégoire Seux | 8ae80a7 | 2019-11-07 11:33:58 +0100 | [diff] [blame] | 91 | it 'should raise TransportError on HTTP failures' do |
| 92 | @client.write "test" |
| 93 | |
| 94 | expect(Net::HTTP).to receive(:new).with("my.domain.com", 80) do |
| 95 | double("Net::HTTP").tap do |http| |
| 96 | expect(http).to receive(:use_ssl=).with(false) |
| 97 | expect(http).to receive(:post).with("/path/to/service?param=value", "test", {"Content-Type"=>"application/x-thrift"}) do |
| 98 | double("Net::HTTPOK").tap do |response| |
| 99 | expect(response).not_to receive(:body) |
| 100 | expect(response).to receive(:code).at_least(:once).and_return "503" |
| 101 | end |
| 102 | end |
| 103 | end |
| 104 | end |
| 105 | |
| 106 | expect { @client.flush }.to raise_error(Thrift::TransportException) |
| 107 | end |
| 108 | |
Kevin Clark | dd33025 | 2008-06-18 01:11:18 +0000 | [diff] [blame] | 109 | end |
Jake Farrell | fbb78a6 | 2013-05-27 22:01:36 -0400 | [diff] [blame] | 110 | |
| 111 | describe 'ssl enabled' do |
| 112 | before(:each) do |
| 113 | @service_path = "/path/to/service?param=value" |
| 114 | @server_uri = "https://my.domain.com" |
| 115 | end |
| 116 | |
| 117 | it "should use SSL for https" do |
| 118 | client = Thrift::HTTPClientTransport.new("#{@server_uri}#{@service_path}") |
| 119 | |
| 120 | client.write "test" |
| 121 | |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 122 | expect(Net::HTTP).to receive(:new).with("my.domain.com", 443) do |
| 123 | double("Net::HTTP").tap do |http| |
| 124 | expect(http).to receive(:use_ssl=).with(true) |
| 125 | expect(http).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER) |
| 126 | expect(http).to receive(:post).with(@service_path, "test", |
| 127 | "Content-Type" => "application/x-thrift") do |
| 128 | double("Net::HTTPOK").tap do |response| |
| 129 | expect(response).to receive(:body).and_return "data" |
Grégoire Seux | 8ae80a7 | 2019-11-07 11:33:58 +0100 | [diff] [blame] | 130 | expect(response).to receive(:code).and_return "200" |
Jake Farrell | fbb78a6 | 2013-05-27 22:01:36 -0400 | [diff] [blame] | 131 | end |
| 132 | end |
| 133 | end |
| 134 | end |
| 135 | client.flush |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 136 | expect(client.read(4)).to eq("data") |
Jake Farrell | fbb78a6 | 2013-05-27 22:01:36 -0400 | [diff] [blame] | 137 | end |
| 138 | |
| 139 | it "should set SSL verify mode when specified" do |
| 140 | client = Thrift::HTTPClientTransport.new("#{@server_uri}#{@service_path}", |
| 141 | :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE) |
| 142 | |
| 143 | client.write "test" |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 144 | expect(Net::HTTP).to receive(:new).with("my.domain.com", 443) do |
| 145 | double("Net::HTTP").tap do |http| |
| 146 | expect(http).to receive(:use_ssl=).with(true) |
| 147 | expect(http).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE) |
| 148 | expect(http).to receive(:post).with(@service_path, "test", |
| 149 | "Content-Type" => "application/x-thrift") do |
| 150 | double("Net::HTTPOK").tap do |response| |
| 151 | expect(response).to receive(:body).and_return "data" |
Grégoire Seux | 8ae80a7 | 2019-11-07 11:33:58 +0100 | [diff] [blame] | 152 | expect(response).to receive(:code).and_return "200" |
Jake Farrell | fbb78a6 | 2013-05-27 22:01:36 -0400 | [diff] [blame] | 153 | end |
| 154 | end |
| 155 | end |
| 156 | end |
| 157 | client.flush |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 158 | expect(client.read(4)).to eq("data") |
Jake Farrell | fbb78a6 | 2013-05-27 22:01:36 -0400 | [diff] [blame] | 159 | end |
| 160 | end |
Kevin Clark | dd33025 | 2008-06-18 01:11:18 +0000 | [diff] [blame] | 161 | end |