Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add test example for unix socket client and update readme #589 #703

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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