1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package runtime
16
17 import (
18 "mime"
19 "net/http"
20 "testing"
21
22 "github.com/go-openapi/errors"
23 "github.com/stretchr/testify/assert"
24 "github.com/stretchr/testify/require"
25 )
26
27 func TestParseContentType(t *testing.T) {
28 _, _, reason1 := mime.ParseMediaType("application(")
29 _, _, reason2 := mime.ParseMediaType("application/json;char*")
30 data := []struct {
31 hdr, mt, cs string
32 err *errors.ParseError
33 }{
34 {"application/json", "application/json", "", nil},
35 {"text/html; charset=utf-8", "text/html", "utf-8", nil},
36 {"text/html;charset=utf-8", "text/html", "utf-8", nil},
37 {"", "application/octet-stream", "", nil},
38 {"text/html; charset=utf-8", "text/html", "utf-8", nil},
39 {"application(", "", "", errors.NewParseError("Content-Type", "header", "application(", reason1)},
40 {"application/json;char*", "", "", errors.NewParseError("Content-Type", "header", "application/json;char*", reason2)},
41 }
42
43 headers := http.Header(map[string][]string{})
44 for _, v := range data {
45 if v.hdr != "" {
46 headers.Set("Content-Type", v.hdr)
47 } else {
48 headers.Del("Content-Type")
49 }
50 ct, cs, err := ContentType(headers)
51 if v.err == nil {
52 require.NoError(t, err, "input: %q, err: %v", v.hdr, err)
53 } else {
54 require.Error(t, err, "input: %q", v.hdr)
55 assert.IsType(t, &errors.ParseError{}, err, "input: %q", v.hdr)
56 assert.Equal(t, v.err.Error(), err.Error(), "input: %q", v.hdr)
57 }
58 assert.Equal(t, v.mt, ct, "input: %q", v.hdr)
59 assert.Equal(t, v.cs, cs, "input: %q", v.hdr)
60 }
61 }
62
View as plain text