github.com/khulnasoft-lab/khulnasoft@v26.0.1-0.20240328202558-330a6f959fe0+incompatible/api/server/httputils/httputils_test.go (about) 1 package httputils // import "github.com/docker/docker/api/server/httputils" 2 3 import ( 4 "net/http" 5 "strings" 6 "testing" 7 ) 8 9 // matchesContentType 10 func TestJsonContentType(t *testing.T) { 11 err := matchesContentType("application/json", "application/json") 12 if err != nil { 13 t.Error(err) 14 } 15 16 err = matchesContentType("application/json; charset=utf-8", "application/json") 17 if err != nil { 18 t.Error(err) 19 } 20 21 expected := "unsupported Content-Type header (dockerapplication/json): must be 'application/json'" 22 err = matchesContentType("dockerapplication/json", "application/json") 23 if err == nil || err.Error() != expected { 24 t.Errorf(`expected "%s", got "%v"`, expected, err) 25 } 26 27 expected = "malformed Content-Type header (foo;;;bar): mime: invalid media parameter" 28 err = matchesContentType("foo;;;bar", "application/json") 29 if err == nil || err.Error() != expected { 30 t.Errorf(`expected "%s", got "%v"`, expected, err) 31 } 32 } 33 34 func TestReadJSON(t *testing.T) { 35 t.Run("nil body", func(t *testing.T) { 36 req, err := http.NewRequest(http.MethodPost, "https://example.com/some/path", nil) 37 if err != nil { 38 t.Error(err) 39 } 40 foo := struct{}{} 41 err = ReadJSON(req, &foo) 42 if err != nil { 43 t.Error(err) 44 } 45 }) 46 47 t.Run("empty body", func(t *testing.T) { 48 req, err := http.NewRequest(http.MethodPost, "https://example.com/some/path", strings.NewReader("")) 49 if err != nil { 50 t.Error(err) 51 } 52 foo := struct{ SomeField string }{} 53 err = ReadJSON(req, &foo) 54 if err != nil { 55 t.Error(err) 56 } 57 if foo.SomeField != "" { 58 t.Errorf("expected: '', got: %s", foo.SomeField) 59 } 60 }) 61 62 t.Run("with valid request", func(t *testing.T) { 63 req, err := http.NewRequest(http.MethodPost, "https://example.com/some/path", strings.NewReader(`{"SomeField":"some value"}`)) 64 if err != nil { 65 t.Error(err) 66 } 67 req.Header.Set("Content-Type", "application/json") 68 foo := struct{ SomeField string }{} 69 err = ReadJSON(req, &foo) 70 if err != nil { 71 t.Error(err) 72 } 73 if foo.SomeField != "some value" { 74 t.Errorf("expected: 'some value', got: %s", foo.SomeField) 75 } 76 }) 77 t.Run("with whitespace", func(t *testing.T) { 78 req, err := http.NewRequest(http.MethodPost, "https://example.com/some/path", strings.NewReader(` 79 80 {"SomeField":"some value"} 81 82 `)) 83 if err != nil { 84 t.Error(err) 85 } 86 req.Header.Set("Content-Type", "application/json") 87 foo := struct{ SomeField string }{} 88 err = ReadJSON(req, &foo) 89 if err != nil { 90 t.Error(err) 91 } 92 if foo.SomeField != "some value" { 93 t.Errorf("expected: 'some value', got: %s", foo.SomeField) 94 } 95 }) 96 97 t.Run("with extra content", func(t *testing.T) { 98 req, err := http.NewRequest(http.MethodPost, "https://example.com/some/path", strings.NewReader(`{"SomeField":"some value"} and more content`)) 99 if err != nil { 100 t.Error(err) 101 } 102 req.Header.Set("Content-Type", "application/json") 103 foo := struct{ SomeField string }{} 104 err = ReadJSON(req, &foo) 105 if err == nil { 106 t.Error("expected an error, got none") 107 } 108 expected := "unexpected content after JSON" 109 if err.Error() != expected { 110 t.Errorf("expected: '%s', got: %s", expected, err.Error()) 111 } 112 }) 113 114 t.Run("invalid JSON", func(t *testing.T) { 115 req, err := http.NewRequest(http.MethodPost, "https://example.com/some/path", strings.NewReader(`{invalid json`)) 116 if err != nil { 117 t.Error(err) 118 } 119 req.Header.Set("Content-Type", "application/json") 120 foo := struct{ SomeField string }{} 121 err = ReadJSON(req, &foo) 122 if err == nil { 123 t.Error("expected an error, got none") 124 } 125 expected := "invalid JSON: invalid character 'i' looking for beginning of object key string" 126 if err.Error() != expected { 127 t.Errorf("expected: '%s', got: %s", expected, err.Error()) 128 } 129 }) 130 }