]>
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 | package thrift | |
21 | ||
22 | import ( | |
23 | "net/http" | |
24 | "testing" | |
25 | ) | |
26 | ||
27 | func TestHttpClient(t *testing.T) { | |
28 | l, addr := HttpClientSetupForTest(t) | |
29 | if l != nil { | |
30 | defer l.Close() | |
31 | } | |
32 | trans, err := NewTHttpPostClient("http://" + addr.String()) | |
33 | if err != nil { | |
34 | l.Close() | |
35 | t.Fatalf("Unable to connect to %s: %s", addr.String(), err) | |
36 | } | |
37 | TransportTest(t, trans, trans) | |
38 | } | |
39 | ||
40 | func TestHttpClientHeaders(t *testing.T) { | |
41 | l, addr := HttpClientSetupForTest(t) | |
42 | if l != nil { | |
43 | defer l.Close() | |
44 | } | |
45 | trans, err := NewTHttpPostClient("http://" + addr.String()) | |
46 | if err != nil { | |
47 | l.Close() | |
48 | t.Fatalf("Unable to connect to %s: %s", addr.String(), err) | |
49 | } | |
50 | TransportHeaderTest(t, trans, trans) | |
51 | } | |
52 | ||
53 | func TestHttpCustomClient(t *testing.T) { | |
54 | l, addr := HttpClientSetupForTest(t) | |
55 | if l != nil { | |
56 | defer l.Close() | |
57 | } | |
58 | ||
59 | httpTransport := &customHttpTransport{} | |
60 | ||
61 | trans, err := NewTHttpPostClientWithOptions("http://"+addr.String(), THttpClientOptions{ | |
62 | Client: &http.Client{ | |
63 | Transport: httpTransport, | |
64 | }, | |
65 | }) | |
66 | if err != nil { | |
67 | l.Close() | |
68 | t.Fatalf("Unable to connect to %s: %s", addr.String(), err) | |
69 | } | |
70 | TransportHeaderTest(t, trans, trans) | |
71 | ||
72 | if !httpTransport.hit { | |
73 | t.Fatalf("Custom client was not used") | |
74 | } | |
75 | } | |
76 | ||
77 | func TestHttpCustomClientPackageScope(t *testing.T) { | |
78 | l, addr := HttpClientSetupForTest(t) | |
79 | if l != nil { | |
80 | defer l.Close() | |
81 | } | |
82 | httpTransport := &customHttpTransport{} | |
83 | DefaultHttpClient = &http.Client{ | |
84 | Transport: httpTransport, | |
85 | } | |
86 | ||
87 | trans, err := NewTHttpPostClient("http://" + addr.String()) | |
88 | if err != nil { | |
89 | l.Close() | |
90 | t.Fatalf("Unable to connect to %s: %s", addr.String(), err) | |
91 | } | |
92 | TransportHeaderTest(t, trans, trans) | |
93 | ||
94 | if !httpTransport.hit { | |
95 | t.Fatalf("Custom client was not used") | |
96 | } | |
97 | } | |
98 | ||
99 | type customHttpTransport struct { | |
100 | hit bool | |
101 | } | |
102 | ||
103 | func (c *customHttpTransport) RoundTrip(req *http.Request) (*http.Response, error) { | |
104 | c.hit = true | |
105 | return http.DefaultTransport.RoundTrip(req) | |
106 | } |