blob: b6a8f4cd7166f3b6ce9b480762c327921fe503db [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
Jake Farrella87810f2012-09-28 01:59:04 +000023 describe Thrift::HTTPClientTransport do
Kevin Clarkdd330252008-06-18 01:11:18 +000024 before(:each) do
Jake Farrella87810f2012-09-28 01:59:04 +000025 @client = Thrift::HTTPClientTransport.new("http://my.domain.com/path/to/service?param=value")
Kevin Clarkdd330252008-06-18 01:11:18 +000026 end
Dmytro Shteflyukf5c80a42026-03-08 19:09:43 -040027
James E. King III9aaf2952018-03-20 15:06:08 -040028 it "should provide a reasonable to_s" do
29 @client.to_s == "http://my.domain.com/path/to/service?param=value"
30 end
Kevin Clarkdd330252008-06-18 01:11:18 +000031
32 it "should always be open" do
James E. King III27247072018-03-22 20:50:23 -040033 expect(@client).to be_open
Kevin Clarkdd330252008-06-18 01:11:18 +000034 @client.close
James E. King III27247072018-03-22 20:50:23 -040035 expect(@client).to be_open
Kevin Clarkdd330252008-06-18 01:11:18 +000036 end
37
38 it "should post via HTTP and return the results" do
39 @client.write "a test"
40 @client.write " frame"
James E. King III27247072018-03-22 20:50:23 -040041 expect(Net::HTTP).to receive(:new).with("my.domain.com", 80) do
42 double("Net::HTTP").tap do |http|
43 expect(http).to receive(:use_ssl=).with(false)
44 expect(http).to receive(:post).with("/path/to/service?param=value", "a test frame", {"Content-Type"=>"application/x-thrift"}) do
45 double("Net::HTTPOK").tap do |response|
46 expect(response).to receive(:body).and_return "data"
Grégoire Seux8ae80a72019-11-07 11:33:58 +010047 expect(response).to receive(:code).and_return "200"
Roger Meiera30930f2012-05-11 18:08:58 +000048 end
49 end
Kevin Clarkdd330252008-06-18 01:11:18 +000050 end
51 end
52 @client.flush
James E. King III27247072018-03-22 20:50:23 -040053 expect(@client.read(10)).to eq("data")
Kevin Clarkdd330252008-06-18 01:11:18 +000054 end
Bryan Duxburyad776c12010-08-05 22:12:01 +000055
56 it "should send custom headers if defined" do
57 @client.write "test"
58 custom_headers = {"Cookie" => "Foo"}
59 headers = {"Content-Type"=>"application/x-thrift"}.merge(custom_headers)
60
61 @client.add_headers(custom_headers)
James E. King III27247072018-03-22 20:50:23 -040062 expect(Net::HTTP).to receive(:new).with("my.domain.com", 80) do
63 double("Net::HTTP").tap do |http|
64 expect(http).to receive(:use_ssl=).with(false)
65 expect(http).to receive(:post).with("/path/to/service?param=value", "test", headers) do
66 double("Net::HTTPOK").tap do |response|
67 expect(response).to receive(:body).and_return "data"
Grégoire Seux8ae80a72019-11-07 11:33:58 +010068 expect(response).to receive(:code).and_return "200"
Roger Meiera30930f2012-05-11 18:08:58 +000069 end
70 end
Bryan Duxburyad776c12010-08-05 22:12:01 +000071 end
72 end
73 @client.flush
74 end
John Thomaseacbd652016-07-12 08:06:19 -070075
76 it 'should reset the outbuf on HTTP failures' do
77 @client.write "test"
78
James E. King III27247072018-03-22 20:50:23 -040079 expect(Net::HTTP).to receive(:new).with("my.domain.com", 80) do
80 double("Net::HTTP").tap do |http|
81 expect(http).to receive(:use_ssl=).with(false)
82 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 -070083 end
84 end
85
Dmytro Shteflyuk3b0ab4d2026-03-11 17:46:48 -040086 @client.flush rescue
James E. King III27247072018-03-22 20:50:23 -040087 expect(@client.instance_variable_get(:@outbuf)).to eq(Thrift::Bytes.empty_byte_buffer)
John Thomaseacbd652016-07-12 08:06:19 -070088 end
89
Grégoire Seux8ae80a72019-11-07 11:33:58 +010090 it 'should raise TransportError on HTTP failures' do
91 @client.write "test"
92
93 expect(Net::HTTP).to receive(:new).with("my.domain.com", 80) do
94 double("Net::HTTP").tap do |http|
95 expect(http).to receive(:use_ssl=).with(false)
96 expect(http).to receive(:post).with("/path/to/service?param=value", "test", {"Content-Type"=>"application/x-thrift"}) do
97 double("Net::HTTPOK").tap do |response|
98 expect(response).not_to receive(:body)
99 expect(response).to receive(:code).at_least(:once).and_return "503"
100 end
101 end
102 end
103 end
104
105 expect { @client.flush }.to raise_error(Thrift::TransportException)
106 end
Kevin Clarkdd330252008-06-18 01:11:18 +0000107 end
Jake Farrellfbb78a62013-05-27 22:01:36 -0400108
109 describe 'ssl enabled' do
110 before(:each) do
111 @service_path = "/path/to/service?param=value"
112 @server_uri = "https://my.domain.com"
113 end
114
115 it "should use SSL for https" do
116 client = Thrift::HTTPClientTransport.new("#{@server_uri}#{@service_path}")
117
118 client.write "test"
119
James E. King III27247072018-03-22 20:50:23 -0400120 expect(Net::HTTP).to receive(:new).with("my.domain.com", 443) do
121 double("Net::HTTP").tap do |http|
122 expect(http).to receive(:use_ssl=).with(true)
123 expect(http).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER)
124 expect(http).to receive(:post).with(@service_path, "test",
Stan Hucfc46022022-11-28 11:29:50 -0800125 {"Content-Type" => "application/x-thrift"}) do
James E. King III27247072018-03-22 20:50:23 -0400126 double("Net::HTTPOK").tap do |response|
127 expect(response).to receive(:body).and_return "data"
Grégoire Seux8ae80a72019-11-07 11:33:58 +0100128 expect(response).to receive(:code).and_return "200"
Jake Farrellfbb78a62013-05-27 22:01:36 -0400129 end
130 end
131 end
132 end
133 client.flush
James E. King III27247072018-03-22 20:50:23 -0400134 expect(client.read(4)).to eq("data")
Jake Farrellfbb78a62013-05-27 22:01:36 -0400135 end
136
137 it "should set SSL verify mode when specified" do
138 client = Thrift::HTTPClientTransport.new("#{@server_uri}#{@service_path}",
139 :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE)
140
141 client.write "test"
James E. King III27247072018-03-22 20:50:23 -0400142 expect(Net::HTTP).to receive(:new).with("my.domain.com", 443) do
143 double("Net::HTTP").tap do |http|
144 expect(http).to receive(:use_ssl=).with(true)
145 expect(http).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
146 expect(http).to receive(:post).with(@service_path, "test",
Stan Hucfc46022022-11-28 11:29:50 -0800147 {"Content-Type" => "application/x-thrift"}) do
James E. King III27247072018-03-22 20:50:23 -0400148 double("Net::HTTPOK").tap do |response|
149 expect(response).to receive(:body).and_return "data"
Grégoire Seux8ae80a72019-11-07 11:33:58 +0100150 expect(response).to receive(:code).and_return "200"
Jake Farrellfbb78a62013-05-27 22:01:36 -0400151 end
152 end
153 end
154 end
155 client.flush
James E. King III27247072018-03-22 20:50:23 -0400156 expect(client.read(4)).to eq("data")
Jake Farrellfbb78a62013-05-27 22:01:36 -0400157 end
158 end
Kevin Clarkdd330252008-06-18 01:11:18 +0000159end