Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HaraldNordgren committed Dec 2, 2024
1 parent 221ad76 commit 4901914
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
10 changes: 9 additions & 1 deletion internal/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,21 @@ func TestSubscriptionConnectionParams(t *testing.T) {
opts []graphql.WebSocketOption
}{
{
name: "authorized_user_gets_counter",
name: "connection_params_authorized_user_gets_counter",
opts: []graphql.WebSocketOption{
graphql.WithConnectionParams(map[string]interface{}{
authKey: "authorized-user-token",
}),
},
},
{
name: "http_header_authorized_user_gets_counter",
opts: []graphql.WebSocketOption{
graphql.WithWebsocketHeader(http.Header{
authKey: []string{"authorized-user-token"},
}),
},
},
{
name: "unauthorized_user_gets_error",
expectedError: "input: countAuthorized unauthorized\n",
Expand Down
19 changes: 18 additions & 1 deletion internal/integration/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"time"
Expand Down Expand Up @@ -198,6 +199,20 @@ func getAuthToken(ctx context.Context) string {
return ""
}

func authHeaderMiddleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

token := r.Header.Get(AuthKey)
if token != "" {
ctx = withAuthToken(ctx, token)
}

r = r.WithContext(ctx)
handler.ServeHTTP(w, r)
})
}

func RunServer() *httptest.Server {
gqlgenServer := handler.New(NewExecutableSchema(Config{Resolvers: &resolver{}}))
gqlgenServer.AddTransport(transport.POST{})
Expand All @@ -217,7 +232,9 @@ func RunServer() *httptest.Server {
return next(ctx)
})

return httptest.NewServer(gqlgenServer)
server := authHeaderMiddleware(gqlgenServer)

return httptest.NewServer(server)
}

type (
Expand Down

0 comments on commit 4901914

Please sign in to comment.