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" |
Roger Meier | a30930f | 2012-05-11 18:08:58 +0000 | [diff] [blame] | 48 | end |
| 49 | end |
Kevin Clark | dd33025 | 2008-06-18 01:11:18 +0000 | [diff] [blame] | 50 | end |
| 51 | end |
| 52 | @client.flush |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 53 | expect(@client.read(10)).to eq("data") |
Kevin Clark | dd33025 | 2008-06-18 01:11:18 +0000 | [diff] [blame] | 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) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 62 | 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" |
Roger Meier | a30930f | 2012-05-11 18:08:58 +0000 | [diff] [blame] | 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 | |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 78 | expect(Net::HTTP).to receive(:new).with("my.domain.com", 80) do |
| 79 | double("Net::HTTP").tap do |http| |
| 80 | expect(http).to receive(:use_ssl=).with(false) |
| 81 | 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] | 82 | end |
| 83 | end |
| 84 | |
| 85 | @client.flush rescue |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 86 | 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] | 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 | |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 102 | expect(Net::HTTP).to receive(:new).with("my.domain.com", 443) do |
| 103 | double("Net::HTTP").tap do |http| |
| 104 | expect(http).to receive(:use_ssl=).with(true) |
| 105 | expect(http).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER) |
| 106 | expect(http).to receive(:post).with(@service_path, "test", |
| 107 | "Content-Type" => "application/x-thrift") do |
| 108 | double("Net::HTTPOK").tap do |response| |
| 109 | expect(response).to receive(:body).and_return "data" |
Jake Farrell | fbb78a6 | 2013-05-27 22:01:36 -0400 | [diff] [blame] | 110 | end |
| 111 | end |
| 112 | end |
| 113 | end |
| 114 | client.flush |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 115 | expect(client.read(4)).to eq("data") |
Jake Farrell | fbb78a6 | 2013-05-27 22:01:36 -0400 | [diff] [blame] | 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" |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 123 | expect(Net::HTTP).to receive(:new).with("my.domain.com", 443) do |
| 124 | double("Net::HTTP").tap do |http| |
| 125 | expect(http).to receive(:use_ssl=).with(true) |
| 126 | expect(http).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE) |
| 127 | expect(http).to receive(:post).with(@service_path, "test", |
| 128 | "Content-Type" => "application/x-thrift") do |
| 129 | double("Net::HTTPOK").tap do |response| |
| 130 | expect(response).to receive(:body).and_return "data" |
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 | end |
Kevin Clark | dd33025 | 2008-06-18 01:11:18 +0000 | [diff] [blame] | 139 | end |