package http_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/google/go-cmp/cmp"
repository "source.toby3d.me/toby3d/auth/internal/client/repository/http"
"source.toby3d.me/toby3d/auth/internal/common"
"source.toby3d.me/toby3d/auth/internal/domain"
)
const testBody string = `
%[1]s
`
func TestGet(t *testing.T) {
t.Parallel()
client := domain.TestClient(t)
srv := httptest.NewUnstartedServer(testHandler(t, *client))
srv.EnableHTTP2 = true
srv.StartTLS()
t.Cleanup(srv.Close)
client.ID = *domain.TestClientID(t, srv.URL+"/")
clients := repository.NewHTTPClientRepository(srv.Client())
result, err := clients.Get(context.Background(), client.ID)
if err != nil {
t.Fatal(err)
}
if out := client.ID; !result.ID.IsEqual(out) {
t.Errorf("GET %s = %s, want %s", client.ID, out, result.ID)
}
if !cmp.Equal(result.Name, client.Name) {
t.Errorf("GET %s = %+s, want %+s", client.ID, result.Name, client.Name)
}
if !cmp.Equal(result.URL, client.URL) {
t.Errorf("GET %s = %+s, want %+s", client.ID, result.URL, client.URL)
}
if !cmp.Equal(result.Logo, client.Logo) {
t.Errorf("GET %s = %+s, want %+s", client.ID, result.Logo, client.Logo)
}
if !cmp.Equal(result.RedirectURI, client.RedirectURI) {
t.Errorf("GET %s = %+s, want %+s", client.ID, result.RedirectURI, client.RedirectURI)
}
}
func testHandler(tb testing.TB, client domain.Client) http.Handler {
tb.Helper()
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(common.HeaderContentType, common.MIMETextHTMLCharsetUTF8)
w.Header().Set(common.HeaderLink, `<`+client.RedirectURI[0].String()+`>; rel="redirect_uri"`)
fmt.Fprintf(w, testBody, client.Name[0], client.URL[0], client.Logo[0], client.RedirectURI[1])
})
}