Skip to content

Commit

Permalink
Merge pull request #1610 from chirino/openapi-work
Browse files Browse the repository at this point in the history
Openapi work
  • Loading branch information
mergify[bot] authored Nov 8, 2023
2 parents 29c270d + 71f4cb8 commit 89c8a26
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 11 deletions.
32 changes: 25 additions & 7 deletions internal/cucumber/http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
//
// Assert that a response header matches the provided text:
//
// Then the response header "Content-Type" should match "application/json;stream=watch"
// Then the response header "Content-Type" should match "application/json;stream=watch"
//
// Assert that a json field of the response body is correct matches the provided json:
//
Expand Down Expand Up @@ -76,6 +76,7 @@ func init() {
ctx.Step(`^I delete the \${([^"]*)} "([^"]*)" key$`, s.iDeleteTheMapKey)
ctx.Step(`^the "(.*)" selection from the response should match "([^"]*)"$`, s.theSelectionFromTheResponseShouldMatch)
ctx.Step(`^the response header "([^"]*)" should match "([^"]*)"$`, s.theResponseHeaderShouldMatch)
ctx.Step(`^the response header "([^"]*)" should start with "([^"]*)"$`, s.theResponseHeaderShouldStartWith)
ctx.Step(`^the "([^"]*)" selection from the response should match json:$`, s.theSelectionFromTheResponseShouldMatchJson)
ctx.Step(`^\${([^"]*)} is not empty$`, s.vpc_idIsNotEmpty)
ctx.Step(`^"([^"]*)" should match "([^"]*)"$`, s.textShouldMatchText)
Expand Down Expand Up @@ -106,12 +107,12 @@ func (s *TestScenario) TheResponseShouldMatchJsonDoc(expected *godog.DocString)
return s.theResponseShouldMatchJson(expected.Content)
}
func (s *TestScenario) theResponseShouldMatchJson(expected string) error {
session := s.Session()
ct := session.Resp.Header.Get("Content-Type")
if !strings.HasPrefix(ct, "application/json") {
return fmt.Errorf("Content-Type is not application/json, but: %s", ct)
err := s.theResponseHeaderShouldStartWith("Content-Type", "application/json")
if err != nil {
return err
}

session := s.Session()
if len(session.RespBytes) == 0 {
return fmt.Errorf("got an empty response from server, expected a json body")
}
Expand All @@ -124,6 +125,10 @@ func (s *TestScenario) TheResponseShouldContainJsonDoc(expected *godog.DocString
}

func (s *TestScenario) theResponseShouldContainJson(expected string) error {
err := s.theResponseHeaderShouldStartWith("Content-Type", "application/json")
if err != nil {
return err
}
session := s.Session()

if len(session.RespBytes) == 0 {
Expand Down Expand Up @@ -194,14 +199,27 @@ func (s *TestScenario) theResponseShouldMatchText(expected string) error {

func (s *TestScenario) theResponseHeaderShouldMatch(header, expected string) error {
session := s.Session()
expanded, err := s.Expand(expected, "defs", "ref")
expanded, err := s.Expand(expected)
if err != nil {
return err
}

actual := session.Resp.Header.Get(header)
if expanded != actual {
return fmt.Errorf("reponse header '%s' does not match expected: %v, actual: %v", header, expanded, actual)
return fmt.Errorf("reponse header '%s' does not match expected: %v, actual: %v, body:\n%s", header, expanded, actual, string(session.RespBytes))
}
return nil
}
func (s *TestScenario) theResponseHeaderShouldStartWith(header, prefix string) error {
session := s.Session()
prefix, err := s.Expand(prefix)
if err != nil {
return err
}

actual := session.Resp.Header.Get(header)
if !strings.HasPrefix(actual, prefix) {
return fmt.Errorf("reponse header '%s' does not start with prefix: %v, actual: %v, body:\n%s", header, prefix, actual, string(session.RespBytes))
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2438,7 +2438,7 @@
"application/json"
],
"tags": [
"RegKey"
"Auth"
],
"summary": "gets the jwks",
"operationId": "Certs",
Expand Down
2 changes: 1 addition & 1 deletion internal/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2104,7 +2104,7 @@ paths:
$ref: '#/definitions/models.InternalServerError'
summary: gets the jwks
tags:
- RegKey
- Auth
/device/login/start:
post:
consumes:
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/reg_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (api *API) DeleteRegKey(c *gin.Context) {
// @Summary gets the jwks
// @Description gets the jwks that can be used to verify JWTs created by this server.
// @Id Certs
// @Tags RegKey
// @Tags Auth
// @Accept json
// @Produce json
// @Success 200 {object} interface{}
Expand Down
9 changes: 9 additions & 0 deletions internal/routers/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"context"
"crypto/tls"
"github.com/go-session/session/v3"
"github.com/nexodus-io/nexodus/internal/docs"
"github.com/nexodus-io/nexodus/pkg/ginsession"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap/zapcore"
"net/http"
"net/url"
"strings"
"time"

Expand Down Expand Up @@ -63,6 +65,13 @@ func NewAPIRouter(ctx context.Context, o APIRouterOptions) (*gin.Engine, error)

newPrometheus().Use(r)

u, err := url.Parse(o.Api.URL)
if err != nil {
return nil, err
}
docs.SwaggerInfo.Schemes = []string{u.Scheme}
docs.SwaggerInfo.Host = u.Host

r.GET("/openapi/*any", ginSwagger.WrapHandler(swaggerFiles.Handler), loggerMiddleware)

device := r.Group("/device", loggerMiddleware)
Expand Down

0 comments on commit 89c8a26

Please sign in to comment.