blob: 292c7521ebb110c88617d95a1c574dafbc7da69f [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001#
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 Farrella87810f2012-09-28 01:59:04 +000020require 'spec_helper'
Kevin Clarkdd330252008-06-18 01:11:18 +000021
Jake Farrella87810f2012-09-28 01:59:04 +000022describe 'Thrift::HTTPClientTransport' do
Kevin Clarkdd330252008-06-18 01:11:18 +000023
Jake Farrella87810f2012-09-28 01:59:04 +000024 describe Thrift::HTTPClientTransport do
Kevin Clarkdd330252008-06-18 01:11:18 +000025 before(:each) do
Jake Farrella87810f2012-09-28 01:59:04 +000026 @client = Thrift::HTTPClientTransport.new("http://my.domain.com/path/to/service?param=value")
Kevin Clarkdd330252008-06-18 01:11:18 +000027 end
James E. King III9aaf2952018-03-20 15:06:08 -040028
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 Clarkdd330252008-06-18 01:11:18 +000032
33 it "should always be open" do
James E. King III27247072018-03-22 20:50:23 -040034 expect(@client).to be_open
Kevin Clarkdd330252008-06-18 01:11:18 +000035 @client.close
James E. King III27247072018-03-22 20:50:23 -040036 expect(@client).to be_open
Kevin Clarkdd330252008-06-18 01:11:18 +000037 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 III27247072018-03-22 20:50:23 -040042 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 Seux8ae80a72019-11-07 11:33:58 +010048 expect(response).to receive(:code).and_return "200"
Roger Meiera30930f2012-05-11 18:08:58 +000049 end
50 end
Kevin Clarkdd330252008-06-18 01:11:18 +000051 end
52 end
53 @client.flush
James E. King III27247072018-03-22 20:50:23 -040054 expect(@client.read(10)).to eq("data")
Kevin Clarkdd330252008-06-18 01:11:18 +000055 end
Bryan Duxburyad776c12010-08-05 22:12:01 +000056
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 III27247072018-03-22 20:50:23 -040063 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 Seux8ae80a72019-11-07 11:33:58 +010069 expect(response).to receive(:code).and_return "200"
Roger Meiera30930f2012-05-11 18:08:58 +000070 end
71 end
Bryan Duxburyad776c12010-08-05 22:12:01 +000072 end
73 end
74 @client.flush
75 end
John Thomaseacbd652016-07-12 08:06:19 -070076
77 it 'should reset the outbuf on HTTP failures' do
78 @client.write "test"
79
James E. King III27247072018-03-22 20:50:23 -040080 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 Thomaseacbd652016-07-12 08:06:19 -070084 end
85 end
86
87 @client.flush rescue
James E. King III27247072018-03-22 20:50:23 -040088 expect(@client.instance_variable_get(:@outbuf)).to eq(Thrift::Bytes.empty_byte_buffer)
John Thomaseacbd652016-07-12 08:06:19 -070089 end
90
Grégoire Seux8ae80a72019-11-07 11:33:58 +010091 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 Clarkdd330252008-06-18 01:11:18 +0000109 end
Jake Farrellfbb78a62013-05-27 22:01:36 -0400110
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 III27247072018-03-22 20:50:23 -0400122 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 Seux8ae80a72019-11-07 11:33:58 +0100130 expect(response).to receive(:code).and_return "200"
Jake Farrellfbb78a62013-05-27 22:01:36 -0400131 end
132 end
133 end
134 end
135 client.flush
James E. King III27247072018-03-22 20:50:23 -0400136 expect(client.read(4)).to eq("data")
Jake Farrellfbb78a62013-05-27 22:01:36 -0400137 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 III27247072018-03-22 20:50:23 -0400144 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 Seux8ae80a72019-11-07 11:33:58 +0100152 expect(response).to receive(:code).and_return "200"
Jake Farrellfbb78a62013-05-27 22:01:36 -0400153 end
154 end
155 end
156 end
157 client.flush
James E. King III27247072018-03-22 20:50:23 -0400158 expect(client.read(4)).to eq("data")
Jake Farrellfbb78a62013-05-27 22:01:36 -0400159 end
160 end
Kevin Clarkdd330252008-06-18 01:11:18 +0000161end