Satisfy WebSocket client lint rules
This commit is contained in:
parent
564823e1a4
commit
201490999b
|
|
@ -26,7 +26,10 @@ import (
|
|||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
const defaultHTTPTimeout = 30 * time.Second
|
||||
const (
|
||||
defaultHTTPTimeout = 30 * time.Second
|
||||
waitParameterName = "wait"
|
||||
)
|
||||
|
||||
type APIError struct {
|
||||
StatusCode int
|
||||
|
|
@ -340,30 +343,6 @@ func (client *Client) wsRequestRaw(
|
|||
return conn, nil
|
||||
}
|
||||
|
||||
func (client *Client) httpClientForWebSocket(params map[string]string) *http.Client {
|
||||
waitRaw, ok := params["wait"]
|
||||
if !ok {
|
||||
return client.httpClient
|
||||
}
|
||||
|
||||
waitSeconds, err := strconv.ParseUint(waitRaw, 10, 16)
|
||||
if err != nil {
|
||||
return client.httpClient
|
||||
}
|
||||
|
||||
waitTimeout := time.Duration(waitSeconds)*time.Second + defaultHTTPTimeout
|
||||
if client.httpClient.Timeout == 0 || client.httpClient.Timeout >= waitTimeout {
|
||||
return client.httpClient
|
||||
}
|
||||
|
||||
return &http.Client{
|
||||
CheckRedirect: client.httpClient.CheckRedirect,
|
||||
Jar: client.httpClient.Jar,
|
||||
Timeout: waitTimeout,
|
||||
Transport: client.httpClient.Transport,
|
||||
}
|
||||
}
|
||||
|
||||
func (client *Client) formatPath(path string) *url.URL {
|
||||
endpointURL := &url.URL{
|
||||
Scheme: client.baseURL.Scheme,
|
||||
|
|
@ -424,3 +403,27 @@ func (client *Client) RPC() *RPCService {
|
|||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (client *Client) httpClientForWebSocket(params map[string]string) *http.Client {
|
||||
waitRaw, ok := params[waitParameterName]
|
||||
if !ok {
|
||||
return client.httpClient
|
||||
}
|
||||
|
||||
waitSeconds, err := strconv.ParseUint(waitRaw, 10, 16)
|
||||
if err != nil {
|
||||
return client.httpClient
|
||||
}
|
||||
|
||||
waitTimeout := time.Duration(waitSeconds)*time.Second + defaultHTTPTimeout
|
||||
if client.httpClient.Timeout == 0 || client.httpClient.Timeout >= waitTimeout {
|
||||
return client.httpClient
|
||||
}
|
||||
|
||||
return &http.Client{
|
||||
CheckRedirect: client.httpClient.CheckRedirect,
|
||||
Jar: client.httpClient.Jar,
|
||||
Timeout: waitTimeout,
|
||||
Transport: client.httpClient.Transport,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ func TestHTTPClientForWebSocketHonorsWait(t *testing.T) {
|
|||
devClient, err := New(WithAddress("http://localhost"))
|
||||
require.NoError(t, err)
|
||||
|
||||
httpClient := devClient.httpClientForWebSocket(map[string]string{"wait": "120"})
|
||||
httpClient := devClient.httpClientForWebSocket(map[string]string{waitParameterName: "120"})
|
||||
|
||||
require.Equal(t, 150*time.Second, httpClient.Timeout)
|
||||
require.Same(t, devClient.httpClient.Transport, httpClient.Transport)
|
||||
|
|
@ -28,7 +28,12 @@ func TestExecSessionBuildsReconnectableQuery(t *testing.T) {
|
|||
query = request.URL.Query()
|
||||
|
||||
conn, err := websocket.Accept(writer, request, nil)
|
||||
require.NoError(t, err)
|
||||
if err != nil {
|
||||
t.Errorf("failed to accept WebSocket connection: %v", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
defer conn.CloseNow()
|
||||
}),
|
||||
)
|
||||
|
|
@ -58,6 +63,6 @@ func TestExecSessionBuildsReconnectableQuery(t *testing.T) {
|
|||
require.Equal(t, []string{"80"}, query["cols"])
|
||||
require.Equal(t, []string{"hello"}, query["env[GREETING]"])
|
||||
require.Equal(t, []string{"/tmp"}, query["workdir"])
|
||||
require.Equal(t, []string{"7"}, query["wait"])
|
||||
require.Equal(t, []string{"7"}, query[waitParameterName])
|
||||
require.Equal(t, []string{"resume-me"}, query["session"])
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue