Skip to content

Commit

Permalink
Add tests for property examples usage in mock responses
Browse files Browse the repository at this point in the history
  • Loading branch information
martinsirbe authored and daveshanley committed Jun 14, 2024
1 parent dfacb38 commit 3548ed6
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions mock/mock_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1323,3 +1323,136 @@ paths:
assert.Equal(t, http.StatusNoContent, status)
assert.Empty(t, b)
}

func TestNewMockEngine_GenerateResponse_CombinedExampleObject(t *testing.T) {
spec := `openapi: 3.0.3
info:
title: Example API
description: An example API for testing purposes
version: 1.0.0
paths:
/examples:
get:
summary: Get example data
description: Retrieve an example response with various fields
responses:
'200':
description: Ok
content:
application/json:
schema:
$ref: '#/components/schemas/Response'
components:
schemas:
ID:
type: integer
Response:
type: object
required:
- id
- name
properties:
id:
$ref: '#/components/schemas/ID'
name:
type: string
username:
type: string
active:
type: boolean
balance:
type: number
format: float
tags:
type: array
items:
type: string
example:
id: 123
name: "John Doe"
username: "jack"
active: true
balance: 99.99
tags: ["tag1", "tag2", "tag3"]`

d, _ := libopenapi.NewDocument([]byte(spec))
doc, _ := d.BuildV3Model()

me := NewMockEngine(&doc.Model, false)

request, err := http.NewRequest(http.MethodGet, "https://api.pb33f.io/examples", http.NoBody)
require.NoError(t, err)

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

assert.Equal(t, http.StatusOK, status)
assert.Equal(t, `{"active":true,"balance":99.99,"id":123,"name":"John Doe","tags":["tag1","tag2","tag3"],"username":"jack"}`, string(b))
}

func TestNewMockEngine_GenerateResponse_IndividualPropertyExamples(t *testing.T) {
spec := `openapi: 3.0.3
info:
title: Example API
description: An example API for testing purposes
version: 1.0.0
paths:
/examples:
get:
summary: Get example data
description: Retrieve an example response with various fields
responses:
'200':
description: Ok
content:
application/json:
schema:
$ref: '#/components/schemas/Response'
components:
schemas:
ID:
type: integer
example: 123
Response:
type: object
required:
- id
- name
properties:
id:
$ref: '#/components/schemas/ID'
name:
type: string
example: "John Doe"
username:
type: string
example: "jack"
active:
type: boolean
example: true
balance:
type: number
format: float
example: 99.99
tags:
type: array
items:
type: string
example: ["tag1", "tag2", "tag3"]`

d, _ := libopenapi.NewDocument([]byte(spec))
doc, _ := d.BuildV3Model()

me := NewMockEngine(&doc.Model, false)

request, err := http.NewRequest(http.MethodGet, "https://api.pb33f.io/examples", http.NoBody)
require.NoError(t, err)

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

assert.Equal(t, http.StatusOK, status)
assert.Equal(t, `{"active":true,"balance":99.99,"id":123,"name":"John Doe","tags":["tag1","tag2","tag3"],"username":"jack"}`, string(b))
}

0 comments on commit 3548ed6

Please sign in to comment.