blob: 793fc73abad6cd4e46fba975a9bd004a710a4790 [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
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 Farrell5d6bd5a2012-10-01 18:42:23 +000039 mock("Net::HTTP").tap do |http|
Kevin Clark6ca38812008-10-16 19:14:47 +000040 http.should_receive(:use_ssl=).with(false)
Roger Meiera30930f2012-05-11 18:08:58 +000041 http.should_receive(:post).with("/path/to/service?param=value", "a test frame", {"Content-Type"=>"application/x-thrift"}).and_return do
Jake Farrell5d6bd5a2012-10-01 18:42:23 +000042 mock("Net::HTTPOK").tap do |response|
Roger Meiera30930f2012-05-11 18:08:58 +000043 response.should_receive(:body).and_return "data"
44 end
45 end
Kevin Clarkdd330252008-06-18 01:11:18 +000046 end
47 end
48 @client.flush
49 @client.read(10).should == "data"
50 end
Bryan Duxburyad776c12010-08-05 22:12:01 +000051
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 Farrell5d6bd5a2012-10-01 18:42:23 +000059 mock("Net::HTTP").tap do |http|
Bryan Duxburyad776c12010-08-05 22:12:01 +000060 http.should_receive(:use_ssl=).with(false)
Roger Meiera30930f2012-05-11 18:08:58 +000061 http.should_receive(:post).with("/path/to/service?param=value", "test", headers).and_return do
Jake Farrell5d6bd5a2012-10-01 18:42:23 +000062 mock("Net::HTTPOK").tap do |response|
Roger Meiera30930f2012-05-11 18:08:58 +000063 response.should_receive(:body).and_return "data"
64 end
65 end
Bryan Duxburyad776c12010-08-05 22:12:01 +000066 end
67 end
68 @client.flush
69 end
Kevin Clarkdd330252008-06-18 01:11:18 +000070 end
Jake Farrellfbb78a62013-05-27 22:01:36 -040071
72 describe 'ssl enabled' do
73 before(:each) do
74 @service_path = "/path/to/service?param=value"
75 @server_uri = "https://my.domain.com"
76 end
77
78 it "should use SSL for https" do
79 client = Thrift::HTTPClientTransport.new("#{@server_uri}#{@service_path}")
80
81 client.write "test"
82
83 Net::HTTP.should_receive(:new).with("my.domain.com", 443).and_return do
84 mock("Net::HTTP").tap do |http|
85 http.should_receive(:use_ssl=).with(true)
86 http.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER)
87 http.should_receive(:post).with(@service_path, "test",
88 "Content-Type" => "application/x-thrift").and_return do
89 mock("Net::HTTPOK").tap do |response|
90 response.should_receive(:body).and_return "data"
91 end
92 end
93 end
94 end
95 client.flush
96 client.read(4).should == "data"
97 end
98
99 it "should set SSL verify mode when specified" do
100 client = Thrift::HTTPClientTransport.new("#{@server_uri}#{@service_path}",
101 :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE)
102
103 client.write "test"
104 Net::HTTP.should_receive(:new).with("my.domain.com", 443).and_return do
105 mock("Net::HTTP").tap do |http|
106 http.should_receive(:use_ssl=).with(true)
107 http.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
108 http.should_receive(:post).with(@service_path, "test",
109 "Content-Type" => "application/x-thrift").and_return do
110 mock("Net::HTTPOK").tap do |response|
111 response.should_receive(:body).and_return "data"
112 end
113 end
114 end
115 end
116 client.flush
117 client.read(4).should == "data"
118 end
119 end
Kevin Clarkdd330252008-06-18 01:11:18 +0000120end