]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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 | ||
20 | require 'spec_helper' | |
21 | ||
22 | describe 'Thrift::HTTPClientTransport' do | |
23 | ||
24 | describe Thrift::HTTPClientTransport do | |
25 | before(:each) do | |
26 | @client = Thrift::HTTPClientTransport.new("http://my.domain.com/path/to/service?param=value") | |
27 | end | |
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 | |
32 | ||
33 | it "should always be open" do | |
34 | expect(@client).to be_open | |
35 | @client.close | |
36 | expect(@client).to 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 | 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" | |
48 | end | |
49 | end | |
50 | end | |
51 | end | |
52 | @client.flush | |
53 | expect(@client.read(10)).to eq("data") | |
54 | end | |
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 | 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" | |
68 | end | |
69 | end | |
70 | end | |
71 | end | |
72 | @client.flush | |
73 | end | |
74 | ||
75 | it 'should reset the outbuf on HTTP failures' do | |
76 | @client.write "test" | |
77 | ||
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 } | |
82 | end | |
83 | end | |
84 | ||
85 | @client.flush rescue | |
86 | expect(@client.instance_variable_get(:@outbuf)).to eq(Thrift::Bytes.empty_byte_buffer) | |
87 | end | |
88 | ||
89 | end | |
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 | 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" | |
110 | end | |
111 | end | |
112 | end | |
113 | end | |
114 | client.flush | |
115 | expect(client.read(4)).to eq("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 | 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" | |
131 | end | |
132 | end | |
133 | end | |
134 | end | |
135 | client.flush | |
136 | expect(client.read(4)).to eq("data") | |
137 | end | |
138 | end | |
139 | end |