Skip to content

Commit

Permalink
test: add test example for unix socket client and update readme #589 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Sep 24, 2023
1 parent 8d8419f commit 4801bed
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -864,7 +864,7 @@ client := resty.New()
client.SetTransport(&transport).SetScheme("http").SetHostURL(unixSocket)

// No need to write the host's URL on the request, just the path.
client.R().Get("/index.html")
client.R().Get("http://localhost/index.html")
```

#### Bazel Support
Expand Down
28 changes: 28 additions & 0 deletions client_test.go
Expand Up @@ -1017,3 +1017,31 @@ func TestPostRedirectWithBody(t *testing.T) {
}
wg.Wait()
}

func TestUnixSocket(t *testing.T) {
unixSocketAddr := createUnixSocketEchoServer(t)
defer os.Remove(unixSocketAddr)

// Create a Go's http.Transport so we can set it in resty.
transport := http.Transport{
Dial: func(_, _ string) (net.Conn, error) {
return net.Dial("unix", unixSocketAddr)
},
}

// Create a Resty Client
client := New()

// Set the previous transport that we created, set the scheme of the communication to the
// socket and set the unixSocket as the HostURL.
client.SetTransport(&transport).SetScheme("http").SetHostURL(unixSocketAddr)

// No need to write the host's URL on the request, just the path.
res, err := client.R().Get("http://localhost/")
assertNil(t, err)
assertEqual(t, "Hi resty client from a server running on Unix domain socket!", res.String())

res, err = client.R().Get("http://localhost/hello")
assertNil(t, err)
assertEqual(t, "Hello resty client from a server running on endpoint /hello!", res.String())
}
29 changes: 29 additions & 0 deletions resty_test.go
Expand Up @@ -13,6 +13,7 @@ import (
"errors"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -630,6 +631,34 @@ func createRedirectServer(t *testing.T) *httptest.Server {
return ts
}

func createUnixSocketEchoServer(t *testing.T) string {
socketPath := filepath.Join(os.TempDir(), strconv.FormatInt(time.Now().Unix(), 10)) + ".sock"

// Create a Unix domain socket and listen for incoming connections.
socket, err := net.Listen("unix", socketPath)
if err != nil {
t.Fatal(err)
}

m := http.NewServeMux()
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hi resty client from a server running on Unix domain socket!\n"))
})

m.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello resty client from a server running on endpoint /hello!\n"))
})

go func(t *testing.T) {
server := http.Server{Handler: m}
if err := server.Serve(socket); err != nil {
t.Error(err)
}
}(t)

return socketPath
}

type digestServerConfig struct {
realm, qop, nonce, opaque, algo, uri, charset, username, password string
}
Expand Down

0 comments on commit 4801bed

Please sign in to comment.