File: body_test.go

package info (click to toggle)
golang-github-go-openapi-runtime 0.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 944 kB
  • sloc: sh: 9; makefile: 4
file content (102 lines) | stat: -rw-r--r-- 2,480 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package middleware

import (
	"io"
	"net/http"
	"path"
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/go-openapi/runtime"
	"github.com/go-openapi/runtime/internal/testing/petstore"
)

type eofReader struct {
}

func (r *eofReader) Read(b []byte) (int, error) {
	return 0, io.EOF
}

func (r *eofReader) Close() error {
	return nil
}

type rbn func(*http.Request, *MatchedRoute) error

func (b rbn) BindRequest(r *http.Request, rr *MatchedRoute) error {
	return b(r, rr)
}

func TestBindRequest_BodyValidation(t *testing.T) {
	spec, api := petstore.NewAPI(t)
	ctx := NewContext(spec, api, nil)
	api.DefaultConsumes = runtime.JSONMime
	ctx.router = DefaultRouter(spec, ctx.api)

	req, err := http.NewRequest("GET", path.Join(spec.BasePath(), "/pets"), new(eofReader))
	if assert.NoError(t, err) {
		req.Header.Set("Content-Type", runtime.JSONMime)

		ri, rCtx, ok := ctx.RouteInfo(req)
		if assert.True(t, ok) {
			req = rCtx
			err := ctx.BindValidRequest(req, ri, rbn(func(r *http.Request, rr *MatchedRoute) error {
				defer r.Body.Close()
				var data interface{}
				err := runtime.JSONConsumer().Consume(r.Body, &data)
				_ = data
				return err
			}))

			assert.Error(t, err)
			assert.Equal(t, io.EOF, err)
		}
	}
}

func TestBindRequest_DeleteNoBody(t *testing.T) {
	spec, api := petstore.NewAPI(t)
	ctx := NewContext(spec, api, nil)
	api.DefaultConsumes = runtime.JSONMime
	ctx.router = DefaultRouter(spec, ctx.api)

	req, err := http.NewRequest("DELETE", path.Join(spec.BasePath(), "/pets/123"), new(eofReader))
	if assert.NoError(t, err) {
		req.Header.Set("Accept", "*/*")

		ri, rCtx, ok := ctx.RouteInfo(req)
		if assert.True(t, ok) {
			req = rCtx
			bverr := ctx.BindValidRequest(req, ri, rbn(func(r *http.Request, rr *MatchedRoute) error {
				return nil
			}))

			assert.NoError(t, bverr)
			//assert.Equal(t, io.EOF, bverr)
		}
	}

	req, err = http.NewRequest("DELETE", path.Join(spec.BasePath(), "/pets/123"), new(eofReader))
	if assert.NoError(t, err) {
		req.Header.Set("Accept", "*/*")
		req.Header.Set("Content-Type", runtime.JSONMime)
		req.ContentLength = 1

		ri, rCtx, ok := ctx.RouteInfo(req)
		if assert.True(t, ok) {
			req = rCtx
			err := ctx.BindValidRequest(req, ri, rbn(func(r *http.Request, rr *MatchedRoute) error {
				defer r.Body.Close()
				var data interface{}
				err := runtime.JSONConsumer().Consume(r.Body, &data)
				_ = data
				return err
			}))

			assert.Error(t, err)
			assert.Equal(t, io.EOF, err)
		}
	}
}