blob: eba366815caa840cc6b816d5761448dd012169b3 [file] [log] [blame]
Christian Lavoieafc6d8f2011-02-20 02:39:19 +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
Jens Geyer0e87c462013-06-18 22:25:07 +020020package thrift
Christian Lavoieafc6d8f2011-02-20 02:39:19 +000021
22import (
Morozov44617282020-12-15 10:35:57 +030023 "bytes"
24 "context"
Justin Larrabee13ac77d2015-11-17 11:20:35 -070025 "net/http"
Jens Geyer0e87c462013-06-18 22:25:07 +020026 "testing"
Christian Lavoieafc6d8f2011-02-20 02:39:19 +000027)
28
29func TestHttpClient(t *testing.T) {
Jens Geyer0e87c462013-06-18 22:25:07 +020030 l, addr := HttpClientSetupForTest(t)
31 if l != nil {
32 defer l.Close()
33 }
34 trans, err := NewTHttpPostClient("http://" + addr.String())
35 if err != nil {
36 l.Close()
Morozov44617282020-12-15 10:35:57 +030037 t.Fatalf("Unable to connect to %s: %v", addr.String(), err)
Jens Geyer0e87c462013-06-18 22:25:07 +020038 }
39 TransportTest(t, trans, trans)
Renan I. Del Valleaa9e7e82020-02-13 11:20:04 -080040
41 t.Run("nilBuffer", func(t *testing.T) {
42 _ = trans.Close()
43 if _, err = trans.Write([]byte{1, 2, 3, 4}); err == nil {
Morozov44617282020-12-15 10:35:57 +030044 t.Fatal("writing to a closed transport did not result in an error")
Renan I. Del Valleaa9e7e82020-02-13 11:20:04 -080045 }
46 })
Christian Lavoieafc6d8f2011-02-20 02:39:19 +000047}
Jens Geyer706cb4e2014-03-19 00:37:10 +020048
49func TestHttpClientHeaders(t *testing.T) {
50 l, addr := HttpClientSetupForTest(t)
51 if l != nil {
52 defer l.Close()
53 }
54 trans, err := NewTHttpPostClient("http://" + addr.String())
55 if err != nil {
56 l.Close()
Morozov44617282020-12-15 10:35:57 +030057 t.Fatalf("Unable to connect to %s: %v", addr.String(), err)
Jens Geyer706cb4e2014-03-19 00:37:10 +020058 }
59 TransportHeaderTest(t, trans, trans)
60}
Justin Larrabee13ac77d2015-11-17 11:20:35 -070061
62func TestHttpCustomClient(t *testing.T) {
63 l, addr := HttpClientSetupForTest(t)
64 if l != nil {
65 defer l.Close()
66 }
67
68 httpTransport := &customHttpTransport{}
69
70 trans, err := NewTHttpPostClientWithOptions("http://"+addr.String(), THttpClientOptions{
71 Client: &http.Client{
72 Transport: httpTransport,
73 },
74 })
75 if err != nil {
76 l.Close()
Morozov44617282020-12-15 10:35:57 +030077 t.Fatalf("Unable to connect to %s: %v", addr.String(), err)
Justin Larrabee13ac77d2015-11-17 11:20:35 -070078 }
79 TransportHeaderTest(t, trans, trans)
80
81 if !httpTransport.hit {
82 t.Fatalf("Custom client was not used")
83 }
84}
85
86func TestHttpCustomClientPackageScope(t *testing.T) {
87 l, addr := HttpClientSetupForTest(t)
88 if l != nil {
89 defer l.Close()
90 }
91 httpTransport := &customHttpTransport{}
92 DefaultHttpClient = &http.Client{
93 Transport: httpTransport,
94 }
95
96 trans, err := NewTHttpPostClient("http://" + addr.String())
97 if err != nil {
98 l.Close()
Morozov44617282020-12-15 10:35:57 +030099 t.Fatalf("Unable to connect to %s: %v", addr.String(), err)
Justin Larrabee13ac77d2015-11-17 11:20:35 -0700100 }
101 TransportHeaderTest(t, trans, trans)
102
103 if !httpTransport.hit {
104 t.Fatalf("Custom client was not used")
105 }
106}
107
Morozov44617282020-12-15 10:35:57 +0300108func TestHTTPClientFlushesRequestBufferOnErrors(t *testing.T) {
109 var (
110 write1 = []byte("write 1")
111 write2 = []byte("write 2")
112 )
113
114 l, addr := HttpClientSetupForTest(t)
115 if l != nil {
116 defer l.Close()
117 }
118 trans, err := NewTHttpPostClient("http://" + addr.String())
119 if err != nil {
120 t.Fatalf("Unable to connect to %s: %v", addr.String(), err)
121 }
122 defer trans.Close()
123
124 _, err = trans.Write(write1)
125 if err != nil {
126 t.Fatalf("Failed to write to transport: %v", err)
127 }
128 ctx, cancel := context.WithCancel(context.Background())
129 cancel()
130 err = trans.Flush(ctx)
131 if err == nil {
132 t.Fatal("Expected flush error")
133 }
134
135 _, err = trans.Write(write2)
136 if err != nil {
137 t.Fatalf("Failed to write to transport: %v", err)
138 }
139 err = trans.Flush(context.Background())
140 if err != nil {
141 t.Fatalf("Failed to flush: %v", err)
142 }
143
144 data := make([]byte, 1024)
145 n, err := trans.Read(data)
146 if err != nil {
147 t.Fatalf("Failed to read: %v", err)
148 }
149
150 data = data[:n]
151 if !bytes.Equal(data, write2) {
152 t.Fatalf("Received unexpected data: %q, expected: %q", data, write2)
153 }
154}
155
Justin Larrabee13ac77d2015-11-17 11:20:35 -0700156type customHttpTransport struct {
157 hit bool
158}
159
160func (c *customHttpTransport) RoundTrip(req *http.Request) (*http.Response, error) {
161 c.hit = true
162 return http.DefaultTransport.RoundTrip(req)
163}