You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

72 lines
2.9 KiB

  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file or at
  5. // https://developers.google.com/open-source/licenses/bsd.
  6. package httputil_test
  7. import (
  8. "github.com/golang/gddo/httputil"
  9. "net/http"
  10. "testing"
  11. )
  12. var negotiateContentEncodingTests = []struct {
  13. s string
  14. offers []string
  15. expect string
  16. }{
  17. {"", []string{"identity", "gzip"}, "identity"},
  18. {"*;q=0", []string{"identity", "gzip"}, ""},
  19. {"gzip", []string{"identity", "gzip"}, "gzip"},
  20. }
  21. func TestNegotiateContentEnoding(t *testing.T) {
  22. for _, tt := range negotiateContentEncodingTests {
  23. r := &http.Request{Header: http.Header{"Accept-Encoding": {tt.s}}}
  24. actual := httputil.NegotiateContentEncoding(r, tt.offers)
  25. if actual != tt.expect {
  26. t.Errorf("NegotiateContentEncoding(%q, %#v)=%q, want %q", tt.s, tt.offers, actual, tt.expect)
  27. }
  28. }
  29. }
  30. var negotiateContentTypeTests = []struct {
  31. s string
  32. offers []string
  33. defaultOffer string
  34. expect string
  35. }{
  36. {"text/html, */*;q=0", []string{"x/y"}, "", ""},
  37. {"text/html, */*", []string{"x/y"}, "", "x/y"},
  38. {"text/html, image/png", []string{"text/html", "image/png"}, "", "text/html"},
  39. {"text/html, image/png", []string{"image/png", "text/html"}, "", "image/png"},
  40. {"text/html, image/png; q=0.5", []string{"image/png"}, "", "image/png"},
  41. {"text/html, image/png; q=0.5", []string{"text/html"}, "", "text/html"},
  42. {"text/html, image/png; q=0.5", []string{"foo/bar"}, "", ""},
  43. {"text/html, image/png; q=0.5", []string{"image/png", "text/html"}, "", "text/html"},
  44. {"text/html, image/png; q=0.5", []string{"text/html", "image/png"}, "", "text/html"},
  45. {"text/html;q=0.5, image/png", []string{"image/png"}, "", "image/png"},
  46. {"text/html;q=0.5, image/png", []string{"text/html"}, "", "text/html"},
  47. {"text/html;q=0.5, image/png", []string{"image/png", "text/html"}, "", "image/png"},
  48. {"text/html;q=0.5, image/png", []string{"text/html", "image/png"}, "", "image/png"},
  49. {"image/png, image/*;q=0.5", []string{"image/jpg", "image/png"}, "", "image/png"},
  50. {"image/png, image/*;q=0.5", []string{"image/jpg"}, "", "image/jpg"},
  51. {"image/png, image/*;q=0.5", []string{"image/jpg", "image/gif"}, "", "image/jpg"},
  52. {"image/png, image/*", []string{"image/jpg", "image/gif"}, "", "image/jpg"},
  53. {"image/png, image/*", []string{"image/gif", "image/jpg"}, "", "image/gif"},
  54. {"image/png, image/*", []string{"image/gif", "image/png"}, "", "image/png"},
  55. {"image/png, image/*", []string{"image/png", "image/gif"}, "", "image/png"},
  56. }
  57. func TestNegotiateContentType(t *testing.T) {
  58. for _, tt := range negotiateContentTypeTests {
  59. r := &http.Request{Header: http.Header{"Accept": {tt.s}}}
  60. actual := httputil.NegotiateContentType(r, tt.offers, tt.defaultOffer)
  61. if actual != tt.expect {
  62. t.Errorf("NegotiateContentType(%q, %#v, %q)=%q, want %q", tt.s, tt.offers, tt.defaultOffer, actual, tt.expect)
  63. }
  64. }
  65. }