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