autoneg_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gold
  5. import (
  6. "net/http"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. var (
  11. chrome = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
  12. rdflib = "application/rdf+xml;q=0.9, application/xhtml+xml;q=0.3, text/xml;q=0.2, application/xml;q=0.2, text/html;q=0.3, text/plain;q=0.1, text/n3;q=1.0, application/x-turtle;q=1, text/turtle;q=1"
  13. )
  14. func mockAccept(accept string) (al AcceptList, err error) {
  15. req := &http.Request{}
  16. req.Header = make(http.Header)
  17. req.Header["Accept"] = []string{accept}
  18. myreq := &httpRequest{req, nil, "", "", "", false}
  19. al, err = myreq.Accept()
  20. return
  21. }
  22. func TestNegotiatePicturesOfWebPages(t *testing.T) {
  23. al, err := mockAccept(chrome)
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. contentType, err := al.Negotiate("text/html", "image/png")
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. if contentType != "image/png" {
  32. t.Errorf("got %s expected image/png", contentType)
  33. }
  34. }
  35. func TestNegotiateRDF(t *testing.T) {
  36. al, err := mockAccept(rdflib)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. contentType, err := al.Negotiate(serializerMimes...)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. assert.Equal(t, "text/turtle", contentType)
  45. }
  46. func TestNegotiateFirstMatch(t *testing.T) {
  47. al, err := mockAccept(chrome)
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. contentType, err := al.Negotiate("text/html", "text/plain", "text/n3")
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. if contentType != "text/html" {
  56. t.Errorf("got %s expected text/html", contentType)
  57. }
  58. }
  59. func TestNegotiateSecondMatch(t *testing.T) {
  60. al, err := mockAccept(chrome)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. contentType, err := al.Negotiate("text/n3", "text/plain")
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. if contentType != "text/plain" {
  69. t.Errorf("got %s expected text/plain", contentType)
  70. }
  71. }
  72. func TestNegotiateWildcardMatch(t *testing.T) {
  73. al, err := mockAccept(chrome)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. contentType, err := al.Negotiate("text/n3", "application/rdf+xml")
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. if contentType != "text/n3" {
  82. t.Errorf("got %s expected text/n3", contentType)
  83. }
  84. }
  85. func TestNegotiateInvalidMediaRange(t *testing.T) {
  86. _, err := mockAccept("something/valid, rubbish, other/valid")
  87. if err == nil {
  88. t.Fatal("expected error on obviously invalid media range")
  89. }
  90. }
  91. func TestNegotiateInvalidParam(t *testing.T) {
  92. _, err := mockAccept("text/plain; foo")
  93. if err == nil {
  94. t.Fatal("expected error on ill-formed params")
  95. }
  96. }
  97. func TestNegotiateEmptyAccept(t *testing.T) {
  98. al, err := mockAccept("")
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. _, err = al.Negotiate("text/plain")
  103. if err == nil {
  104. t.Error("expected error with empty but present accept header")
  105. }
  106. }
  107. func TestNegotiateStarAccept(t *testing.T) {
  108. al, err := mockAccept("*")
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. if al[0].Type+"/"+al[0].SubType != "*/*" {
  113. t.Error("expected subtype * for single * accept header")
  114. }
  115. }
  116. func TestNegotiateNoAlternative(t *testing.T) {
  117. al, err := mockAccept(chrome)
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. _, err = al.Negotiate()
  122. if err == nil {
  123. t.Error("expected error with no alternative")
  124. }
  125. }