URL shortener and file dump for hashru.link https://hashru.link
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.
 
 
 
 

55 lines
1.8 KiB

  1. package rushlink
  2. import (
  3. "net/http"
  4. "testing"
  5. )
  6. func resolveResponseContentTypeSuccess(t *testing.T, expected string, types []string, acceptVal string) {
  7. r, err := http.NewRequest("HEAD", "", nil)
  8. if err != nil {
  9. panic(err)
  10. }
  11. r.Header.Set("Accept", acceptVal)
  12. got, err := resolveResponseContentType(r, types)
  13. if err != nil {
  14. t.Errorf("error: '%v'\n", err)
  15. }
  16. if got != expected {
  17. t.Errorf("error: '%v' should be '%v'\n", got, expected)
  18. }
  19. }
  20. func resolveResponseContentTypeError(t *testing.T, expected string, types []string, acceptVal string) {
  21. r, err := http.NewRequest("HEAD", "", nil)
  22. if err != nil {
  23. panic(err)
  24. }
  25. r.Header.Set("Accept", acceptVal)
  26. got, err := resolveResponseContentType(r, types)
  27. if err == nil {
  28. t.Errorf("expected an error, but got a success: '%v'", got)
  29. }
  30. if got != "" {
  31. t.Errorf("error: return value should be empty, not '%v'", got)
  32. }
  33. if err.Error() != expected {
  34. t.Errorf("wrong error value error: got '%v', want '%v'\n", got, expected)
  35. }
  36. }
  37. func TestResolveResponseContentType(t *testing.T) {
  38. resolveResponseContentTypeSuccess(t, "", []string{}, "text/html")
  39. resolveResponseContentTypeSuccess(t, "text/html", []string{"text/html"}, "")
  40. resolveResponseContentTypeSuccess(t, "text/html", []string{"text/txt", "text/html"}, "text/txt;q=0.5,text/html")
  41. resolveResponseContentTypeSuccess(t, "text/html", []string{"text/txt", "text/html"}, "text/txt;q=0.5,text/html;q=0.9")
  42. resolveResponseContentTypeSuccess(t, "", []string{"text"}, "text/html")
  43. resolveResponseContentTypeSuccess(t, "", []string{"text/*"}, "image/*")
  44. // Issue #17
  45. resolveResponseContentTypeSuccess(t, "*/*", []string{"*/*"}, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3")
  46. // Issue #66
  47. resolveResponseContentTypeError(t, "bad media-range ('*')", []string{"text/plain"}, " *")
  48. }