mime_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package mime
  2. import (
  3. "testing"
  4. )
  5. func TestDefault(t *testing.T) {
  6. m := Default()
  7. if m.Essence != "text/html" {
  8. t.Fatalf(`Default media type should be "text/html", not %#v`, m.Essence)
  9. }
  10. if m.Supertype != "text" {
  11. t.Fatalf(`Default media type supertype should be "text", not %#v`, m.Supertype)
  12. }
  13. if m.Subtype != "html" {
  14. t.Fatalf(`Default media type subtype should be "html", not %#v`, m.Subtype)
  15. }
  16. }
  17. func TestFailedParse(t *testing.T) {
  18. m, err := Parse("")
  19. if err == nil {
  20. t.Fatalf("Should fail to parse an empty string, but instead returns: %#v", m)
  21. }
  22. m, err = Parse("application")
  23. if err == nil {
  24. t.Fatalf("Should fail to parse invalid media type, but instead returns: %#v", m)
  25. }
  26. }
  27. func TestSuccessfulUpdate(t *testing.T) {
  28. m := Default()
  29. err := m.Update("application/json ; charset=utf-8")
  30. if err != nil {
  31. t.Fatalf("Update should have succeeded but returned error: %v", err)
  32. }
  33. if m.Essence != "application/json" {
  34. t.Fatalf(`New media type should be "application/json", not %#v`, m.Essence)
  35. }
  36. if m.Supertype != "application" {
  37. t.Fatalf(`New media type supertype should be "application", not %#v`, m.Supertype)
  38. }
  39. if m.Subtype != "json" {
  40. t.Fatalf(`New media type subtype should be "json", not %#v`, m.Subtype)
  41. }
  42. }
  43. func TestFailedUpdate(t *testing.T) {
  44. m := Default()
  45. err := m.Update("no slash")
  46. if err == nil {
  47. t.Fatalf(`Expected "no slash" to result in an Update error, but it resulted in: %#v`, m)
  48. }
  49. }
  50. func TestMatchesSuccess(t *testing.T) {
  51. m := Default()
  52. matches := m.Matches([]string{"application/json", "text/html"})
  53. if !matches {
  54. t.Fatalf(`Expected media type to match text/html but it did not: %#v`, m)
  55. }
  56. }
  57. func TestMatchesFailure(t *testing.T) {
  58. m := Default()
  59. matches := m.Matches([]string{"application/json"})
  60. if matches {
  61. t.Fatalf(`Expected media type to not match application/json: %#v`, m)
  62. }
  63. }