-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simplify code and add more test for better coverage
- Loading branch information
Showing
6 changed files
with
234 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
package dgclient_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/dgate-io/dgate/pkg/dgclient" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDGClient_OptionsWithRedirect(t *testing.T) { | ||
var client = dgclient.NewDGateClient() | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.URL.Path == "/api/v1/service/test" { | ||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect) | ||
return | ||
} | ||
if r.Method == http.MethodGet { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write([]byte(`{"name":"test"}`)) | ||
} | ||
})) | ||
defer server.Close() | ||
|
||
err := client.Init(server.URL, | ||
dgclient.WithHttpClient(server.Client()), | ||
dgclient.WithFollowRedirect(true), | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
_, err = client.GetService("test", "default") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestDGClient_OptionsRedirectError(t *testing.T) { | ||
var client = dgclient.NewDGateClient() | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if user, pass, _ := r.BasicAuth(); user != "user" || pass != "password" { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
if r.URL.Path == "/api/v1/service/test" { | ||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect) | ||
return | ||
} | ||
if r.Method == http.MethodGet { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write([]byte(`{"name":"test"}`)) | ||
} | ||
})) | ||
defer server.Close() | ||
|
||
err := client.Init(server.URL, | ||
dgclient.WithHttpClient(server.Client()), | ||
dgclient.WithBasicAuth("user", "password"), | ||
dgclient.WithFollowRedirect(false), | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
_, err = client.GetService("test", "default") | ||
if err == nil { | ||
t.Fatal("expected error") | ||
} | ||
} | ||
|
||
func TestDGClient_OptionsWithBasicAuth(t *testing.T) { | ||
var client = dgclient.NewDGateClient() | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if user, pass, _ := r.BasicAuth(); user != "user" || pass != "password" { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
if r.Method == http.MethodGet { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write([]byte(`{"name":"test"}`)) | ||
} | ||
})) | ||
defer server.Close() | ||
|
||
err := client.Init(server.URL, | ||
dgclient.WithHttpClient(server.Client()), | ||
dgclient.WithVerboseLogging(true), | ||
dgclient.WithBasicAuth("user", "password"), | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
_, err = client.GetService("test", "default") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestDGClient_OptionsBasicAuthError(t *testing.T) { | ||
var client = dgclient.NewDGateClient() | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if user, pass, _ := r.BasicAuth(); user != "user" || pass != "password" { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
if r.Method == http.MethodGet { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write([]byte(`{"name":"test"}`)) | ||
} | ||
})) | ||
defer server.Close() | ||
|
||
err := client.Init(server.URL, | ||
dgclient.WithHttpClient(server.Client()), | ||
dgclient.WithBasicAuth("user", "wrongpassword"), | ||
dgclient.WithVerboseLogging(true), | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
_, err = client.GetService("test", "default") | ||
if err == nil { | ||
t.Fatal("expected error") | ||
} | ||
} | ||
|
||
func TestDGClient_OptionsWithUserAgent(t *testing.T) { | ||
var client = dgclient.NewDGateClient() | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.UserAgent() != "test" { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
if r.Method == http.MethodGet { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write([]byte(`{"name":"test"}`)) | ||
} | ||
})) | ||
defer server.Close() | ||
|
||
err := client.Init(server.URL, | ||
dgclient.WithHttpClient(server.Client()), | ||
dgclient.WithVerboseLogging(true), | ||
dgclient.WithUserAgent("test"), | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
_, err = client.GetService("test", "default") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestDGClient_OptionsWithUserAgent2(t *testing.T) { | ||
var client = dgclient.NewDGateClient() | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.UserAgent() != "test" { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
if r.Method == http.MethodGet { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write([]byte(`{"name":"test"}`)) | ||
} | ||
})) | ||
defer server.Close() | ||
|
||
err := client.Init(server.URL, | ||
dgclient.WithHttpClient(server.Client()), | ||
dgclient.WithUserAgent("test"), | ||
dgclient.WithVerboseLogging(true), | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
_, err = client.GetService("test", "default") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestDGClient_Init_ParseURLError(t *testing.T) { | ||
var client = dgclient.NewDGateClient() | ||
err := client.Init("://#/:asdm") | ||
if err == nil { | ||
t.Fatal("expected error") | ||
} | ||
} | ||
|
||
func TestDGClient_Init_EmptyHostError(t *testing.T) { | ||
var client = dgclient.NewDGateClient() | ||
err := client.Init("") | ||
if err == nil { | ||
t.Fatal("expected error") | ||
} | ||
assert.Equal(t, "host is empty", err.Error()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.