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 |
| 34 | @client.should be_open |
| 35 | @client.close |
| 36 | @client.should be_open |
| 37 | end |
| 38 | |
| 39 | it "should post via HTTP and return the results" do |
| 40 | @client.write "a test" |
| 41 | @client.write " frame" |
| 42 | Net::HTTP.should_receive(:new).with("my.domain.com", 80).and_return do |
Jake Farrell | 5d6bd5a | 2012-10-01 18:42:23 +0000 | [diff] [blame] | 43 | mock("Net::HTTP").tap do |http| |
Kevin Clark | 6ca3881 | 2008-10-16 19:14:47 +0000 | [diff] [blame] | 44 | http.should_receive(:use_ssl=).with(false) |
Roger Meier | a30930f | 2012-05-11 18:08:58 +0000 | [diff] [blame] | 45 | http.should_receive(:post).with("/path/to/service?param=value", "a test frame", {"Content-Type"=>"application/x-thrift"}).and_return do |
Jake Farrell | 5d6bd5a | 2012-10-01 18:42:23 +0000 | [diff] [blame] | 46 | mock("Net::HTTPOK").tap do |response| |
Roger Meier | a30930f | 2012-05-11 18:08:58 +0000 | [diff] [blame] | 47 | response.should_receive(:body).and_return "data" |
| 48 | end |
| 49 | end |
Kevin Clark | dd33025 | 2008-06-18 01:11:18 +0000 | [diff] [blame] | 50 | end |
| 51 | end |
| 52 | @client.flush |
| 53 | @client.read(10).should == "data" |
| 54 | end |
Bryan Duxbury | ad776c1 | 2010-08-05 22:12:01 +0000 | [diff] [blame] | 55 | |
| 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) |
| 62 | Net::HTTP.should_receive(:new).with("my.domain.com", 80).and_return do |
Jake Farrell | 5d6bd5a | 2012-10-01 18:42:23 +0000 | [diff] [blame] | 63 | mock("Net::HTTP").tap do |http| |
Bryan Duxbury | ad776c1 | 2010-08-05 22:12:01 +0000 | [diff] [blame] | 64 | http.should_receive(:use_ssl=).with(false) |
Roger Meier | a30930f | 2012-05-11 18:08:58 +0000 | [diff] [blame] | 65 | http.should_receive(:post).with("/path/to/service?param=value", "test", headers).and_return do |
Jake Farrell | 5d6bd5a | 2012-10-01 18:42:23 +0000 | [diff] [blame] | 66 | mock("Net::HTTPOK").tap do |response| |
Roger Meier | a30930f | 2012-05-11 18:08:58 +0000 | [diff] [blame] | 67 | response.should_receive(:body).and_return "data" |
| 68 | end |
| 69 | end |
Bryan Duxbury | ad776c1 | 2010-08-05 22:12:01 +0000 | [diff] [blame] | 70 | end |
| 71 | end |
| 72 | @client.flush |
| 73 | end |
John Thomas | eacbd65 | 2016-07-12 08:06:19 -0700 | [diff] [blame] | 74 | |
| 75 | it 'should reset the outbuf on HTTP failures' do |
| 76 | @client.write "test" |
| 77 | |
| 78 | Net::HTTP.should_receive(:new).with("my.domain.com", 80).and_return do |
| 79 | mock("Net::HTTP").tap do |http| |
| 80 | http.should_receive(:use_ssl=).with(false) |
| 81 | http.should_receive(:post).with("/path/to/service?param=value", "test", {"Content-Type"=>"application/x-thrift"}) { raise Net::ReadTimeout } |
| 82 | end |
| 83 | end |
| 84 | |
| 85 | @client.flush rescue |
| 86 | @client.instance_variable_get(:@outbuf).should eq(Thrift::Bytes.empty_byte_buffer) |
| 87 | end |
| 88 | |
Kevin Clark | dd33025 | 2008-06-18 01:11:18 +0000 | [diff] [blame] | 89 | end |
Jake Farrell | fbb78a6 | 2013-05-27 22:01:36 -0400 | [diff] [blame] | 90 | |
| 91 | describe 'ssl enabled' do |
| 92 | before(:each) do |
| 93 | @service_path = "/path/to/service?param=value" |
| 94 | @server_uri = "https://my.domain.com" |
| 95 | end |
| 96 | |
| 97 | it "should use SSL for https" do |
| 98 | client = Thrift::HTTPClientTransport.new("#{@server_uri}#{@service_path}") |
| 99 | |
| 100 | client.write "test" |
| 101 | |
| 102 | Net::HTTP.should_receive(:new).with("my.domain.com", 443).and_return do |
| 103 | mock("Net::HTTP").tap do |http| |
| 104 | http.should_receive(:use_ssl=).with(true) |
| 105 | http.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER) |
| 106 | http.should_receive(:post).with(@service_path, "test", |
| 107 | "Content-Type" => "application/x-thrift").and_return do |
| 108 | mock("Net::HTTPOK").tap do |response| |
| 109 | response.should_receive(:body).and_return "data" |
| 110 | end |
| 111 | end |
| 112 | end |
| 113 | end |
| 114 | client.flush |
| 115 | client.read(4).should == "data" |
| 116 | end |
| 117 | |
| 118 | it "should set SSL verify mode when specified" do |
| 119 | client = Thrift::HTTPClientTransport.new("#{@server_uri}#{@service_path}", |
| 120 | :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE) |
| 121 | |
| 122 | client.write "test" |
| 123 | Net::HTTP.should_receive(:new).with("my.domain.com", 443).and_return do |
| 124 | mock("Net::HTTP").tap do |http| |
| 125 | http.should_receive(:use_ssl=).with(true) |
| 126 | http.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE) |
| 127 | http.should_receive(:post).with(@service_path, "test", |
| 128 | "Content-Type" => "application/x-thrift").and_return do |
| 129 | mock("Net::HTTPOK").tap do |response| |
| 130 | response.should_receive(:body).and_return "data" |
| 131 | end |
| 132 | end |
| 133 | end |
| 134 | end |
| 135 | client.flush |
| 136 | client.read(4).should == "data" |
| 137 | end |
| 138 | end |
Kevin Clark | dd33025 | 2008-06-18 01:11:18 +0000 | [diff] [blame] | 139 | end |