未验证 提交 57fda944 编写于 作者: M Mislav Marohnić 提交者: GitHub

Merge pull request #1845 from apakulov-stripe/unix-socket-support

Add unix socket support
......@@ -28,6 +28,21 @@ func SetupTomlTestConfig() *TestConfigs {
return &TestConfigs{file.Name()}
}
func SetupTomlTestConfigWithUnixSocket() *TestConfigs {
file, _ := ioutil.TempFile("", "test-gh-config-")
content := `[[hosts]]
host = "github.com"
user = "jingweno"
access_token = "123"
protocol = "http"
unix_socket = "/tmp/go.sock"`
ioutil.WriteFile(file.Name(), []byte(content), os.ModePerm)
os.Setenv("HUB_CONFIG", file.Name())
return &TestConfigs{file.Name()}
}
func SetupTestConfigs() *TestConfigs {
file, _ := ioutil.TempFile("", "test-gh-config-")
......@@ -41,3 +56,18 @@ github.com:
return &TestConfigs{file.Name()}
}
func SetupTestConfigsWithUnixSocket() *TestConfigs {
file, _ := ioutil.TempFile("", "test-gh-config-")
content := `---
github.com:
- user: jingweno
oauth_token: 123
protocol: http
unix_socket: /tmp/go.sock`
ioutil.WriteFile(file.Name(), []byte(content), os.ModePerm)
os.Setenv("HUB_CONFIG", file.Name())
return &TestConfigs{file.Name()}
}
......@@ -890,7 +890,8 @@ func (client *Client) simpleApi() (c *simpleClient, err error) {
}
func (client *Client) apiClient() *simpleClient {
httpClient := newHttpClient(os.Getenv("HUB_TEST_HOST"), os.Getenv("HUB_VERBOSE") != "")
unixSocket := os.ExpandEnv(client.Host.UnixSocket)
httpClient := newHttpClient(os.Getenv("HUB_TEST_HOST"), os.Getenv("HUB_VERBOSE") != "", unixSocket)
apiRoot := client.absolute(normalizeHost(client.Host.Host))
if client.Host != nil && client.Host.Host != GitHubHost {
apiRoot.Path = "/api/v3/"
......
......@@ -21,6 +21,7 @@ type yamlHost struct {
User string `yaml:"user"`
OAuthToken string `yaml:"oauth_token"`
Protocol string `yaml:"protocol"`
UnixSocket string `yaml:"unix_socket,omitempty"`
}
type yamlConfig map[string][]yamlHost
......@@ -30,6 +31,7 @@ type Host struct {
User string `toml:"user"`
AccessToken string `toml:"access_token"`
Protocol string `toml:"protocol"`
UnixSocket string `toml:"unix_socket,omitempty"`
}
type Config struct {
......
......@@ -46,6 +46,7 @@ func (y *yamlConfigDecoder) Decode(r io.Reader, c *Config) error {
User: vv.User,
AccessToken: vv.OAuthToken,
Protocol: vv.Protocol,
UnixSocket: vv.UnixSocket,
}
c.Hosts = append(c.Hosts, host)
}
......
......@@ -30,6 +30,7 @@ func (y *yamlConfigEncoder) Encode(w io.Writer, c *Config) error {
User: h.User,
OAuthToken: h.AccessToken,
Protocol: h.Protocol,
UnixSocket: h.UnixSocket,
},
}
}
......
......@@ -30,6 +30,28 @@ func TestConfigService_TomlLoad(t *testing.T) {
assert.Equal(t, "http", host.Protocol)
}
func TestConfigService_TomlLoad_UnixSocket(t *testing.T) {
testConfigUnixSocket := fixtures.SetupTomlTestConfigWithUnixSocket()
defer testConfigUnixSocket.TearDown()
cc := &Config{}
cs := &configService{
Encoder: &tomlConfigEncoder{},
Decoder: &tomlConfigDecoder{},
}
err := cs.Load(testConfigUnixSocket.Path, cc)
assert.Equal(t, nil, err)
assert.Equal(t, 1, len(cc.Hosts))
host := cc.Hosts[0]
assert.Equal(t, "github.com", host.Host)
assert.Equal(t, "jingweno", host.User)
assert.Equal(t, "123", host.AccessToken)
assert.Equal(t, "http", host.Protocol)
assert.Equal(t, "/tmp/go.sock", host.UnixSocket)
}
func TestConfigService_YamlLoad(t *testing.T) {
testConfig := fixtures.SetupTestConfigs()
defer testConfig.TearDown()
......@@ -50,6 +72,28 @@ func TestConfigService_YamlLoad(t *testing.T) {
assert.Equal(t, "http", host.Protocol)
}
func TestConfigService_YamlLoad_Unix_Socket(t *testing.T) {
testConfigUnixSocket := fixtures.SetupTestConfigsWithUnixSocket()
defer testConfigUnixSocket.TearDown()
cc := &Config{}
cs := &configService{
Encoder: &yamlConfigEncoder{},
Decoder: &yamlConfigDecoder{},
}
err := cs.Load(testConfigUnixSocket.Path, cc)
assert.Equal(t, nil, err)
assert.Equal(t, 1, len(cc.Hosts))
host := cc.Hosts[0]
assert.Equal(t, "github.com", host.Host)
assert.Equal(t, "jingweno", host.User)
assert.Equal(t, "123", host.AccessToken)
assert.Equal(t, "http", host.Protocol)
assert.Equal(t, "/tmp/go.sock", host.UnixSocket)
}
func TestConfigService_TomlSave(t *testing.T) {
file, _ := ioutil.TempFile("", "test-gh-config-")
defer os.RemoveAll(file.Name())
......@@ -78,6 +122,36 @@ func TestConfigService_TomlSave(t *testing.T) {
assert.Equal(t, content, strings.TrimSpace(string(b)))
}
func TestConfigService_TomlSave_UnixSocket(t *testing.T) {
file, _ := ioutil.TempFile("", "test-gh-config-")
defer os.RemoveAll(file.Name())
host := &Host{
Host: "github.com",
User: "jingweno",
AccessToken: "123",
Protocol: "https",
UnixSocket: "/tmp/go.sock",
}
c := &Config{Hosts: []*Host{host}}
cs := &configService{
Encoder: &tomlConfigEncoder{},
Decoder: &tomlConfigDecoder{},
}
err := cs.Save(file.Name(), c)
assert.Equal(t, nil, err)
b, _ := ioutil.ReadFile(file.Name())
content := `[[hosts]]
host = "github.com"
user = "jingweno"
access_token = "123"
protocol = "https"
unix_socket = "/tmp/go.sock"`
assert.Equal(t, content, strings.TrimSpace(string(b)))
}
func TestConfigService_YamlSave(t *testing.T) {
file, _ := ioutil.TempFile("", "test-gh-config-")
defer os.RemoveAll(file.Name())
......@@ -104,3 +178,32 @@ func TestConfigService_YamlSave(t *testing.T) {
protocol: https`
assert.Equal(t, content, strings.TrimSpace(string(b)))
}
func TestConfigService_YamlSave_UnixSocket(t *testing.T) {
file, _ := ioutil.TempFile("", "test-gh-config-")
defer os.RemoveAll(file.Name())
host := &Host{
Host: "github.com",
User: "jingweno",
AccessToken: "123",
Protocol: "https",
UnixSocket: "/tmp/go.sock",
}
c := &Config{Hosts: []*Host{host}}
cs := &configService{
Encoder: &yamlConfigEncoder{},
Decoder: &yamlConfigDecoder{},
}
err := cs.Save(file.Name(), c)
assert.Equal(t, nil, err)
b, _ := ioutil.ReadFile(file.Name())
content := `github.com:
- user: jingweno
oauth_token: "123"
protocol: https
unix_socket: /tmp/go.sock`
assert.Equal(t, content, strings.TrimSpace(string(b)))
}
......@@ -135,20 +135,35 @@ func (t *verboseTransport) verbosePrintln(msg string) {
fmt.Fprintln(t.Out, msg)
}
func newHttpClient(testHost string, verbose bool) *http.Client {
func newHttpClient(testHost string, verbose bool, unixSocket string) *http.Client {
var testURL *url.URL
if testHost != "" {
testURL, _ = url.Parse(testHost)
}
tr := &verboseTransport{
Transport: &http.Transport{
var httpTransport *http.Transport
if unixSocket != "" {
dialFunc := func(network, addr string) (net.Conn, error) {
return net.Dial("unix", unixSocket)
}
httpTransport = &http.Transport{
Dial: dialFunc,
DialTLS: dialFunc,
ResponseHeaderTimeout: 30 * time.Second,
ExpectContinueTimeout: 10 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
}
} else {
httpTransport = &http.Transport{
Proxy: proxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
},
}
}
tr := &verboseTransport{
Transport: httpTransport,
Verbose: verbose,
OverrideURL: testURL,
Out: ui.Stderr,
......
......@@ -3,19 +3,32 @@ package github
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"github.com/bmizerany/assert"
)
func setupTestServer() *testServer {
func setupTestServer(unixSocket string) *testServer {
m := http.NewServeMux()
s := httptest.NewServer(m)
u, _ := url.Parse(s.URL)
if unixSocket != "" {
os.Remove(unixSocket)
unixListener, err := net.Listen("unix", unixSocket)
if err != nil {
log.Fatal("Unable to listen on unix-socket: ", err)
}
go http.Serve(unixListener, m)
}
return &testServer{
Server: s,
ServeMux: m,
......@@ -34,7 +47,7 @@ func (s *testServer) Close() {
}
func TestNewHttpClient_OverrideURL(t *testing.T) {
s := setupTestServer()
s := setupTestServer("")
defer s.Close()
s.HandleFunc("/override", func(w http.ResponseWriter, r *http.Request) {
......@@ -42,7 +55,7 @@ func TestNewHttpClient_OverrideURL(t *testing.T) {
assert.Equal(t, "example.com", r.Host)
})
c := newHttpClient(s.URL.String(), false)
c := newHttpClient(s.URL.String(), false, "")
c.Get("https://example.com/override")
s.HandleFunc("/not-override", func(w http.ResponseWriter, r *http.Request) {
......@@ -50,10 +63,25 @@ func TestNewHttpClient_OverrideURL(t *testing.T) {
assert.Equal(t, s.URL.Host, r.Host)
})
c = newHttpClient("", false)
c = newHttpClient("", false, "")
c.Get(fmt.Sprintf("%s/not-override", s.URL.String()))
}
func TestNewHttpClient_UnixSocket(t *testing.T) {
sock := "/tmp/hub-go.sock"
s := setupTestServer(sock)
defer s.Close()
s.HandleFunc("/unix-socket", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("unix-socket-works"))
})
c := newHttpClient("", false, sock)
resp, err := c.Get(fmt.Sprintf("%s/unix-socket", s.URL.String()))
assert.Equal(t, nil, err)
result, _ := ioutil.ReadAll(resp.Body)
assert.Equal(t, "unix-socket-works", string(result))
}
func TestVerboseTransport_VerbosePrintln(t *testing.T) {
var b bytes.Buffer
tr := &verboseTransport{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册