Debian Codegraph @

errors/urlencoded_errors_test.go

raw
1 package errors
2
3 import (
4 "testing"
5
6 "github.com/pb33f/libopenapi"
7 "github.com/pb33f/libopenapi-validator/helpers"
8 "github.com/pb33f/libopenapi/datamodel/high/base"
9 "github.com/stretchr/testify/assert"
10 )
11
12 func getURLEncodingTestSchema() *base.Schema {
13 spec := `openapi: 3.0.0
14 paths:
15 /pet:
16 get:
17 responses:
18 '200':
19 content:
20 application/x-www-form-urlencoded:
21 encoding:
22 animal:
23 contentType: application/json
24 schema:
25 type: object
26 properties:
27 animal:
28 type: object`
29
30 doc, _ := libopenapi.NewDocument([]byte(spec))
31 v3Doc, _ := doc.BuildV3Model()
32
33 return v3Doc.Model.Paths.PathItems.GetOrZero("/pet").Get.Responses.Codes.GetOrZero("200").
34 Content.GetOrZero("application/x-www-form-urlencoded").Schema.Schema()
35 }
36
37 func TestInvalidURLEncodedParsing(t *testing.T) {
38 err := InvalidURLEncodedParsing("no data sent", "invalid-formdata")
39
40 assert.NotNil(t, (*err))
41 assert.Equal(t, (*err).SchemaValidationErrors[0].Reason, "no data sent")
42 assert.Equal(t, (*err).SchemaValidationErrors[0].ReferenceObject, "invalid-formdata")
43 assert.Equal(t, helpers.Schema, (*err).ValidationSubType)
44 }
45
46 func TestInvalidTypeEncoding(t *testing.T) {
47 err := InvalidTypeEncoding(getURLEncodingTestSchema(), "animal", helpers.JSONContentType)
48
49 assert.NotNil(t, (*err))
50 assert.Equal(t, helpers.InvalidTypeEncoding, (*err).ValidationSubType)
51 }
52
53 func TestReservedURLEncodedValue(t *testing.T) {
54 err := ReservedURLEncodedValue(getURLEncodingTestSchema(), "animal", "!")
55
56 assert.NotNil(t, (*err))
57 assert.Equal(t, helpers.ReservedValues, (*err).ValidationSubType)
58 }