Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
macyabbey authored and daveshanley committed Apr 3, 2024
1 parent 3dc943f commit e66c503
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 53 deletions.
3 changes: 2 additions & 1 deletion mock/mock_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ func (rme *ResponseMockEngine) runWorkflow(request *http.Request) ([]byte, int,

if preferred != "" {
// If an explicit preferred header is present, let it have a chance to take precedence
// This can lead to a preferred header leading to a 3xx, 4xx, or 5xx example response.
// This allows a developer to cause a 3xx, 4xx, or 5xx mocked response by passing
// the appropriate example header value.
mt, lo, noMT = rme.findMediaTypeContainingNamedExample(operation, request, preferred)
}

Expand Down
101 changes: 49 additions & 52 deletions mock/mock_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ package mock
import (
"bytes"
"encoding/json"
"io"
"net/http"
"testing"

"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi-validator/helpers"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"testing"
)

// var doc *v3.Document
Expand Down Expand Up @@ -845,7 +844,7 @@ components:
}

// https://github.com/pb33f/wiretap/issues/84
func TestNewMockEngine_UseExamples_Preferred_From_400(t *testing.T) {
func TestNewMockEngine_UseExamples_FromSchema(t *testing.T) {

spec := `openapi: 3.1.0
paths:
Expand All @@ -857,29 +856,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Thing'
examples:
happyDays:
value:
name: happy days
description: a terrible show from a time that never existed.
robocop:
value:
name: robocop
description: perhaps the best cyberpunk movie ever made.
'400':
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorThing'
examples:
sadErrorDays:
value:
name: sad error days
description: a sad error prone show
sadcop:
value:
name: sad cop
description: perhaps the saddest cyberpunk movie ever made.
components:
schemas:
Thing:
Expand All @@ -891,15 +867,6 @@ components:
description:
type: string
example: descriptionExample
ErrorThing:
type: object
properties:
name:
type: string
example: errorNameExample
description:
type: string
example: errorDescriptionExample
`

d, _ := libopenapi.NewDocument([]byte(spec))
Expand All @@ -908,23 +875,22 @@ components:
me := NewMockEngine(&doc.Model, false)

request, _ := http.NewRequest(http.MethodGet, "https://api.pb33f.io/test", nil)
request.Header.Set(helpers.Preferred, "sadcop")

b, status, err := me.GenerateResponse(request)

assert.NoError(t, err)
assert.Equal(t, 400, status)
assert.Equal(t, 200, status)

var decoded map[string]any
_ = json.Unmarshal(b, &decoded)

assert.Equal(t, "sad cop", decoded["name"])
assert.Equal(t, "perhaps the saddest cyberpunk movie ever made.", decoded["description"])
assert.Equal(t, "nameExample", decoded["name"])
assert.Equal(t, "descriptionExample", decoded["description"])

}

// https://github.com/pb33f/wiretap/issues/84
func TestNewMockEngine_UseExamples_FromSchema(t *testing.T) {
func TestNewMockEngine_UseExamples_FromSchema_Generated(t *testing.T) {

spec := `openapi: 3.1.0
paths:
Expand All @@ -943,10 +909,8 @@ components:
properties:
name:
type: string
example: nameExample
description:
type: string
example: descriptionExample
`

d, _ := libopenapi.NewDocument([]byte(spec))
Expand All @@ -964,13 +928,12 @@ components:
var decoded map[string]any
_ = json.Unmarshal(b, &decoded)

assert.Equal(t, "nameExample", decoded["name"])
assert.Equal(t, "descriptionExample", decoded["description"])
assert.NotEmpty(t, decoded["name"])
assert.NotEmpty(t, decoded["description"])

}

// https://github.com/pb33f/wiretap/issues/84
func TestNewMockEngine_UseExamples_FromSchema_Generated(t *testing.T) {
func TestNewMockEngine_UseExamples_Preferred_From_400(t *testing.T) {

spec := `openapi: 3.1.0
paths:
Expand All @@ -982,15 +945,49 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Thing'
examples:
happyDays:
value:
name: happy days
description: a terrible show from a time that never existed.
robocop:
value:
name: robocop
description: perhaps the best cyberpunk movie ever made.
'400':
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorThing'
examples:
sadErrorDays:
value:
name: sad error days
description: a sad error prone show
sadcop:
value:
name: sad cop
description: perhaps the saddest cyberpunk movie ever made.
components:
schemas:
Thing:
type: object
properties:
name:
type: string
example: nameExample
description:
type: string
example: descriptionExample
ErrorThing:
type: object
properties:
name:
type: string
example: errorNameExample
description:
type: string
example: errorDescriptionExample
`

d, _ := libopenapi.NewDocument([]byte(spec))
Expand All @@ -999,16 +996,16 @@ components:
me := NewMockEngine(&doc.Model, false)

request, _ := http.NewRequest(http.MethodGet, "https://api.pb33f.io/test", nil)
request.Header.Set(helpers.Preferred, "sadcop")

b, status, err := me.GenerateResponse(request)

assert.NoError(t, err)
assert.Equal(t, 200, status)
assert.Equal(t, 400, status)

var decoded map[string]any
_ = json.Unmarshal(b, &decoded)

assert.NotEmpty(t, decoded["name"])
assert.NotEmpty(t, decoded["description"])

assert.Equal(t, "sad cop", decoded["name"])
assert.Equal(t, "perhaps the saddest cyberpunk movie ever made.", decoded["description"])
}

0 comments on commit e66c503

Please sign in to comment.