|
| 1 | +// Copyright (c) Jeevanandam M (https://github.com/jeevatkm) |
| 2 | +// go-aah/aah source code and usage is governed by a MIT style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package ahttp |
| 6 | + |
| 7 | +import ( |
| 8 | + "net/url" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "aahframework.org/test/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestNegotiateContentType(t *testing.T) { |
| 15 | + req1 := createRawHTTPRequest(HeaderAccept, "audio/*; q=0.2, audio/basic") |
| 16 | + contentType := NegotiateContentType(req1) |
| 17 | + assert.Equal(t, "audio/basic", contentType.String()) |
| 18 | + assert.Equal(t, "audio/basic", contentType.Mime) |
| 19 | + assert.Equal(t, "", contentType.Version()) |
| 20 | + |
| 21 | + req2 := createRawHTTPRequest(HeaderAccept, "application/json;version=2") |
| 22 | + contentType = NegotiateContentType(req2) |
| 23 | + assert.Equal(t, "application/json; version=2", contentType.String()) |
| 24 | + assert.Equal(t, "application/json", contentType.Mime) |
| 25 | + assert.Equal(t, "2", contentType.Version()) |
| 26 | + |
| 27 | + req3 := createRawHTTPRequest(HeaderAccept, "text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c") |
| 28 | + contentType = NegotiateContentType(req3) |
| 29 | + assert.Equal(t, "text/html", contentType.String()) |
| 30 | + assert.Equal(t, "text/html", contentType.Mime) |
| 31 | + assert.Equal(t, "", contentType.Version()) |
| 32 | + |
| 33 | + req4 := createRawHTTPRequest(HeaderAccept, "") |
| 34 | + contentType = NegotiateContentType(req4) |
| 35 | + assert.Equal(t, "text/html; charset=utf-8", contentType.String()) |
| 36 | + assert.Equal(t, "text/html", contentType.Mime) |
| 37 | + assert.Equal(t, ".html", contentType.Exts[0]) |
| 38 | + assert.Equal(t, "", contentType.Version()) |
| 39 | + |
| 40 | + req := createRawHTTPRequest(HeaderAccept, "application/json") |
| 41 | + req.URL, _ = url.Parse("http://localhost:8080/testpath.json") |
| 42 | + contentType = NegotiateContentType(req) |
| 43 | + assert.Equal(t, "application/json", contentType.Mime) |
| 44 | + assert.Equal(t, ".json", contentType.Exts[0]) |
| 45 | + |
| 46 | + req = createRawHTTPRequest(HeaderAccept, "application/json") |
| 47 | + req.URL, _ = url.Parse("http://localhost:8080/testpath.html") |
| 48 | + contentType = NegotiateContentType(req) |
| 49 | + assert.Equal(t, "text/html; charset=utf-8", contentType.Mime) |
| 50 | + assert.Equal(t, ".html", contentType.Exts[0]) |
| 51 | + |
| 52 | + req = createRawHTTPRequest(HeaderAccept, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") |
| 53 | + contentType = NegotiateContentType(req) |
| 54 | + assert.Equal(t, "text/html", contentType.String()) |
| 55 | + assert.Equal(t, "text/html", contentType.Mime) |
| 56 | + assert.Equal(t, "", contentType.Version()) |
| 57 | + |
| 58 | + // ParseAccept |
| 59 | + req = createRawHTTPRequest(HeaderAccept, "application/json; version=2") |
| 60 | + spec := ParseAccept(req, HeaderAccept).MostQualified() |
| 61 | + assert.Equal(t, "2", spec.GetParam("version", "1")) |
| 62 | + |
| 63 | + req = createRawHTTPRequest(HeaderAccept, "application/json") |
| 64 | + spec = ParseAccept(req, HeaderAccept).MostQualified() |
| 65 | + assert.Equal(t, "1", spec.GetParam("version", "1")) |
| 66 | + |
| 67 | + req = createRawHTTPRequest(HeaderAccept, "application/json; version") |
| 68 | + spec = ParseAccept(req, HeaderAccept).MostQualified() |
| 69 | + assert.Equal(t, "", spec.GetParam("version", "1")) |
| 70 | + |
| 71 | +} |
| 72 | + |
| 73 | +func TestParseContentType(t *testing.T) { |
| 74 | + req1 := createRawHTTPRequest(HeaderContentType, "text/html; charset=utf-8") |
| 75 | + contentType := ParseContentType(req1) |
| 76 | + assert.Equal(t, "text/html", contentType.Mime) |
| 77 | + assert.Equal(t, "text/html; charset=utf-8", contentType.String()) |
| 78 | + assert.Equal(t, "utf-8", contentType.Charset("iso-8859-1")) |
| 79 | + |
| 80 | + req2 := createRawHTTPRequest(HeaderContentType, "text/html") |
| 81 | + contentType = ParseContentType(req2) |
| 82 | + assert.Equal(t, "text/html", contentType.Mime) |
| 83 | + assert.Equal(t, "text/html", contentType.String()) |
| 84 | + assert.Equal(t, "iso-8859-1", contentType.Charset("iso-8859-1")) |
| 85 | + |
| 86 | + req3 := createRawHTTPRequest(HeaderContentType, "application/json") |
| 87 | + contentType = ParseContentType(req3) |
| 88 | + assert.Equal(t, "application/json", contentType.Mime) |
| 89 | + assert.Equal(t, "application/json", contentType.String()) |
| 90 | + assert.Equal(t, "", contentType.Charset("")) |
| 91 | + |
| 92 | + req4 := createRawHTTPRequest(HeaderContentType, "") |
| 93 | + contentType = ParseContentType(req4) |
| 94 | + assert.Equal(t, "text/html", contentType.Mime) |
| 95 | + assert.Equal(t, "text/html; charset=utf-8", contentType.String()) |
| 96 | + assert.Equal(t, ".html", contentType.Exts[0]) |
| 97 | + |
| 98 | + req5 := createRawHTTPRequest(HeaderContentType, "text/html;charset") |
| 99 | + contentType = ParseContentType(req5) |
| 100 | + assert.Equal(t, "text/html", contentType.Mime) |
| 101 | + assert.Equal(t, "text/html; charset=", contentType.String()) |
| 102 | + assert.Equal(t, "", contentType.Charset("iso-8859-1")) |
| 103 | +} |
0 commit comments