Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
hub
提交
57fda944
H
hub
项目概览
github
/
hub
大约 1 年 前同步成功
通知
3
Star
22523
Fork
2406
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
57fda944
编写于
10月 30, 2018
作者:
M
Mislav Marohnić
提交者:
GitHub
10月 30, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1845 from apakulov-stripe/unix-socket-support
Add unix socket support
上级
7d2b328c
0eb76ff8
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
190 addition
and
9 deletion
+190
-9
fixtures/test_configs.go
fixtures/test_configs.go
+30
-0
github/client.go
github/client.go
+2
-1
github/config.go
github/config.go
+2
-0
github/config_decoder.go
github/config_decoder.go
+1
-0
github/config_encoder.go
github/config_encoder.go
+1
-0
github/config_service_test.go
github/config_service_test.go
+103
-0
github/http.go
github/http.go
+19
-4
github/http_test.go
github/http_test.go
+32
-4
未找到文件。
fixtures/test_configs.go
浏览文件 @
57fda944
...
...
@@ -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
()}
}
github/client.go
浏览文件 @
57fda944
...
...
@@ -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/"
...
...
github/config.go
浏览文件 @
57fda944
...
...
@@ -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
{
...
...
github/config_decoder.go
浏览文件 @
57fda944
...
...
@@ -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
)
}
...
...
github/config_encoder.go
浏览文件 @
57fda944
...
...
@@ -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
,
},
}
}
...
...
github/config_service_test.go
浏览文件 @
57fda944
...
...
@@ -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
)))
}
github/http.go
浏览文件 @
57fda944
...
...
@@ -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
,
...
...
github/http_test.go
浏览文件 @
57fda944
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录