1 Star 0 Fork 0

github_repo/trino-golang-client

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
integration_tls_test.go 3.76 KB
一键复制 编辑 原始数据 按行查看 历史
painsOnline 提交于 2022-01-06 17:51 +08:00 . Add files via upload
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build go1.9
package trino
import (
"bytes"
"crypto/tls"
"database/sql"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"sync"
"testing"
)
func TestIntegrationTLS(t *testing.T) {
proxyServer := newTLSReverseProxy(t)
defer proxyServer.Close()
RegisterCustomClient("test_tls", proxyServer.Client())
defer DeregisterCustomClient("test_tls")
dsn := proxyServer.URL + "?custom_client=test_tls"
testSimpleQuery(t, dsn)
}
func TestIntegrationInsecureTLS(t *testing.T) {
proxyServer := newTLSReverseProxy(t)
defer proxyServer.Close()
RegisterCustomClient("test_insecure_tls", &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
})
defer DeregisterCustomClient("test_insecure_tls")
dsn := proxyServer.URL + "?custom_client=test_insecure_tls"
testSimpleQuery(t, dsn)
}
func testSimpleQuery(t *testing.T, dsn string) {
db, err := sql.Open("trino", dsn)
if err != nil {
t.Fatal(err)
}
defer db.Close()
row := db.QueryRow("SELECT 1")
var count int
if err = row.Scan(&count); err != nil {
t.Fatal(err)
}
if count != 1 {
t.Fatal("unexpected count=", count)
}
}
// hax0r reverse tls proxy for integration tests
// newTLSReverseProxy creates a TLS integration test server.
func newTLSReverseProxy(t *testing.T) *httptest.Server {
dsn := integrationServerDSN(t)
serverURL, _ := url.Parse(dsn)
cproxyURL := make(chan string, 1)
handler := newReverseProxyHandler(serverURL, cproxyURL)
srv := httptest.NewTLSServer(http.HandlerFunc(handler))
cproxyURL <- srv.URL
close(cproxyURL)
proxyURL, _ := url.Parse(srv.URL)
proxyURL.User = serverURL.User
proxyURL.Path = serverURL.Path
proxyURL.RawPath = serverURL.RawPath
proxyURL.RawQuery = serverURL.RawQuery
srv.URL = proxyURL.String()
return srv
}
// newReverseProxyHandler creates an http handler that proxies requests to the given serverUrl, and replaces URLs in responses with the first value sent to the cproxyURL channel.
func newReverseProxyHandler(serverURL *url.URL, cproxyURL chan string) http.HandlerFunc {
baseURL := []byte(serverURL.Scheme + "://" + serverURL.Host)
var proxyURL []byte
var onceProxyURL sync.Once
return func(w http.ResponseWriter, r *http.Request) {
onceProxyURL.Do(func() {
proxyURL = []byte(<-cproxyURL)
})
target := *serverURL
target.User = nil
target.Path = r.URL.Path
target.RawPath = r.URL.RawPath
target.RawQuery = r.URL.RawQuery
req, err := http.NewRequest(r.Method, target.String(), r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
for k, v := range r.Header {
if strings.HasPrefix(k, "X-") {
req.Header[k] = v
}
}
client := *http.DefaultClient
client.Timeout = *integrationServerQueryTimeout
resp, err := client.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
defer resp.Body.Close()
w.WriteHeader(resp.StatusCode)
pr, pw := io.Pipe()
go func() {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
pw.CloseWithError(err)
return
}
b = bytes.Replace(b, baseURL, proxyURL, -1)
pw.Write(b)
pw.Close()
}()
io.Copy(w, pr)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/github_repo/trino-golang-client.git
git@gitee.com:github_repo/trino-golang-client.git
github_repo
trino-golang-client
trino-golang-client
main

搜索帮助