Skip to content

Commit 66323b8

Browse files
committed
simpify code, update README
1 parent d922fc8 commit 66323b8

File tree

3 files changed

+36
-69
lines changed

3 files changed

+36
-69
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ func main() {
1616
client := gojsonrpc.NewClient("http://mock.rpcservice.url")
1717

1818
// you can optionally set a HTTP proxy for the connection
19-
client.SetHTTPProxy("http://proxy.url:3128")
19+
proxyURL, _ := "http://proxy.url:3128"
20+
client.SetHTTPProxy(proxyURL)
2021

2122
// you can also optionally set the connection timeout
2223
client.SetTimeout(120)

client.go

+18-37
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,36 @@ import (
1313

1414
// Client executes JSON RPC calls to remote servers.
1515
type Client struct {
16-
URL string
17-
18-
timeout int
19-
proxyURL string
16+
serverURL string
2017
httpClient *http.Client
2118
}
2219

2320
// NewClient returns a newly istantiated Client pointing to the given url.
2421
func NewClient(url string) *Client {
25-
client := &Client{
26-
URL: url,
27-
timeout: defaultTimeout,
28-
proxyURL: "",
22+
httpClient := &http.Client{
23+
Timeout: time.Duration(time.Duration(defaultTimeout) * time.Second),
24+
Transport: &http.Transport{
25+
TLSClientConfig: &tls.Config{
26+
InsecureSkipVerify: true,
27+
},
28+
Proxy: http.ProxyFromEnvironment,
29+
},
2930
}
30-
client.setHTTPClient()
3131

32-
return client
32+
return &Client{
33+
serverURL: url,
34+
httpClient: httpClient,
35+
}
3336
}
3437

3538
// SetTimeout sets the client timeout to the given value.
3639
func (c *Client) SetTimeout(timeout int) {
37-
c.timeout = timeout
38-
c.setHTTPClient()
40+
c.httpClient.Timeout = time.Duration(timeout) * time.Second
3941
}
4042

41-
// SetHTTPProxy tells the client to use the given httpProxyURL as proxy address.
42-
func (c *Client) SetHTTPProxy(httpProxyURL string) {
43-
c.proxyURL = httpProxyURL
44-
c.setHTTPClient()
43+
// SetHTTPProxyURL tells the client to use the given proxyURL as proxy address.
44+
func (c *Client) SetHTTPProxyURL(proxyURL *url.URL) {
45+
c.httpClient.Transport.(*http.Transport).Proxy = http.ProxyURL(proxyURL)
4546
}
4647

4748
// Run executes the given method having the given params setting the response
@@ -92,30 +93,10 @@ func (c *Client) Notify(method string, params interface{}) error {
9293
return nil
9394
}
9495

95-
func (c *Client) setHTTPClient() {
96-
transport := &http.Transport{
97-
TLSClientConfig: &tls.Config{
98-
InsecureSkipVerify: true,
99-
},
100-
Proxy: http.ProxyFromEnvironment,
101-
}
102-
103-
if parsedProxyURL, err := url.Parse(c.proxyURL); c.proxyURL != "" && err == nil {
104-
transport.Proxy = http.ProxyURL(parsedProxyURL)
105-
}
106-
107-
newHTTPClient := &http.Client{
108-
Timeout: time.Duration(time.Duration(c.timeout) * time.Second),
109-
Transport: transport,
110-
}
111-
112-
c.httpClient = newHTTPClient
113-
}
114-
11596
func (c *Client) sendJSONRequest(jsonRequest []byte) ([]byte, error) {
11697
var jsonResponse []byte
11798

118-
httpRequest, err := http.NewRequest("POST", c.URL, strings.NewReader(string(jsonRequest)))
99+
httpRequest, err := http.NewRequest("POST", c.serverURL, strings.NewReader(string(jsonRequest)))
119100
httpRequest.Header.Set("Content-Type", "application/json")
120101
httpRequest.Header.Set("Content-Length", "")
121102
httpRequest.Header.Set("Accept", "application/json")

client_test.go

+16-31
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,30 @@
11
package gojsonrpc
22

33
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"net/url"
47
"testing"
5-
"time"
68
)
79

8-
func TestThatNewClientReturnsTheExpectedClient(t *testing.T) {
9-
mockURL := "http://mock.url"
10+
func TestThatSetHTTPProxyURLSetsAnHTTPProxyInClient(t *testing.T) {
11+
proxyCalled := false
1012

11-
sut := NewClient(mockURL)
13+
mockProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
14+
proxyCalled = true
15+
}))
1216

13-
if sut.timeout != defaultTimeout ||
14-
sut.proxyURL != "" ||
15-
sut.httpClient.Timeout != time.Duration(defaultTimeout)*time.Second {
16-
t.Fatal("expected Client was not received.")
17-
}
18-
}
19-
20-
func TestThatSetTimeoutSucceeds(t *testing.T) {
21-
mockURL := "http://mock.url"
22-
mockTimeout := 123
17+
mockRPCServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
18+
}))
2319

24-
sut := NewClient(mockURL)
25-
sut.SetTimeout(mockTimeout)
26-
27-
if sut.timeout != 123 ||
28-
sut.proxyURL != "" ||
29-
sut.httpClient.Timeout != time.Duration(123)*time.Second {
30-
t.Fatal("expected Client was not received.")
31-
}
32-
}
20+
sut := NewClient(mockRPCServer.URL)
3321

34-
func TestThatSetProxySucceeds(t *testing.T) {
35-
mockURL := "http://mock.url"
36-
mockProxyURL := "http://proxy.url:1234"
22+
mockHTTPProxyURL, _ := url.Parse(mockProxy.URL)
3723

38-
sut := NewClient(mockURL)
39-
sut.SetHTTPProxy(mockProxyURL)
24+
sut.SetHTTPProxyURL(mockHTTPProxyURL)
25+
_ = sut.Notify("SometMethod", nil)
4026

41-
if sut.timeout != defaultTimeout ||
42-
sut.proxyURL != mockProxyURL {
43-
t.Fatal("expected Client was not received.")
27+
if !proxyCalled {
28+
t.Fatal("expected proxy was not called")
4429
}
4530
}

0 commit comments

Comments
 (0)