mime_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package gold
  2. import (
  3. // "os"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. var (
  8. mimeParserExpect = map[string]string{
  9. // "application/json": "internal",
  10. "application/sparql-update": "internal",
  11. "application/ld+json": "jsonld",
  12. "application/rdf+xml": "rdfxml",
  13. "application/rss": "rss-tag-soup",
  14. "application/x-trig": "trig",
  15. "text/n3": "turtle",
  16. "text/turtle": "turtle",
  17. "text/x-nquads": "nquads",
  18. // "application/n-triples": "ntriples",
  19. }
  20. mimeSerializerExpect = map[string]string{
  21. "application/ld+json": "internal",
  22. "text/html": "internal",
  23. "application/atom+xml": "atom",
  24. "application/json": "json",
  25. "application/rdf+xml": "rdfxml-abbrev",
  26. "application/rss+xml": "rss-1.0",
  27. "application/xhtml+xml": "html",
  28. "text/turtle": "turtle",
  29. "text/x-graphviz": "dot",
  30. "text/x-nquads": "nquads",
  31. // "application/n-triples": "ntriples",
  32. }
  33. )
  34. func TestMimeParserExpect(t *testing.T) {
  35. for k, v := range mimeParserExpect {
  36. assert.Equal(t, v, mimeParser[k])
  37. }
  38. }
  39. func TestMimeSerializerExpect(t *testing.T) {
  40. for k, v := range mimeSerializerExpect {
  41. assert.Equal(t, v, mimeSerializer[k])
  42. }
  43. }
  44. func TestMapPathToExtension(t *testing.T) {
  45. // empty nil empty + error msg
  46. path := ""
  47. ctype := ""
  48. res, err := MapPathToExtension(path, ctype)
  49. assert.Error(t, err)
  50. assert.Empty(t, res)
  51. // /space/ nil /space/
  52. path = "/space/"
  53. ctype = ""
  54. res, err = MapPathToExtension(path, ctype)
  55. assert.NoError(t, err)
  56. assert.Equal(t, path, res)
  57. // /space/ text/html /space/
  58. path = "/space/"
  59. ctype = "text/html"
  60. res, err = MapPathToExtension(path, ctype)
  61. assert.NoError(t, err)
  62. assert.Equal(t, path, res)
  63. // /space/foo nil empty + error msg
  64. path = "/space/foo"
  65. ctype = ""
  66. res, err = MapPathToExtension(path, ctype)
  67. assert.Error(t, err)
  68. assert.Empty(t, res)
  69. // /space/foo.html nil /space/foo.html
  70. path = "/space/foo.html"
  71. ctype = ""
  72. res, err = MapPathToExtension(path, ctype)
  73. assert.NoError(t, err)
  74. assert.Equal(t, path, res)
  75. // /space/foo.html text/html /space/foo.html
  76. path = "/space/foo.html"
  77. ctype = "text/html"
  78. res, err = MapPathToExtension(path, ctype)
  79. assert.NoError(t, err)
  80. assert.Equal(t, path, res)
  81. // /space/foo.ttl nil /space/foo.ttl
  82. path = "/space/foo.ttl"
  83. ctype = "text/turtle"
  84. res, err = MapPathToExtension(path, ctype)
  85. assert.NoError(t, err)
  86. assert.Equal(t, path, res)
  87. // /space/foo.html text/turtle /space/foo.html$.ttl
  88. path = "/space/foo.html"
  89. ctype = "text/turtle"
  90. res, err = MapPathToExtension(path, ctype)
  91. assert.NoError(t, err)
  92. assert.Equal(t, path+"$.ttl", res)
  93. // /space/foo text/turtle /space/foo.ttl
  94. path = "/space/foo"
  95. ctype = "text/turtle"
  96. res, err = MapPathToExtension(path, ctype)
  97. assert.NoError(t, err)
  98. assert.Equal(t, path+".ttl", res)
  99. // /space/foo.acl text/turtle /space/foo.acl
  100. path = "/space/foo" + config.ACLSuffix
  101. ctype = "text/turtle"
  102. res, err = MapPathToExtension(path, ctype)
  103. assert.NoError(t, err)
  104. assert.Equal(t, path, res)
  105. // /space/foo.meta text/turtle /space/foo.acl
  106. path = "/space/foo" + config.MetaSuffix
  107. ctype = "text/turtle"
  108. res, err = MapPathToExtension(path, ctype)
  109. assert.NoError(t, err)
  110. assert.Equal(t, path, res)
  111. // /space/foo nil /space/foo.jpg$.htm
  112. path = "/space/foo"
  113. ctype = "text/html"
  114. res, err = MapPathToExtension(path, "")
  115. assert.Error(t, err)
  116. assert.Empty(t, res)
  117. // /space/foo.jpg text/html /space/foo.jpg$.htm
  118. path = "/space/foo.jpg"
  119. ctype = "text/html"
  120. res, err = MapPathToExtension(path, ctype)
  121. assert.NoError(t, err)
  122. assert.Contains(t, res, path+"$.htm")
  123. // /space/foo.exe text/html /space/foo.exe$.htm
  124. path = "/space/foo.exe"
  125. ctype = "text/html"
  126. res, err = MapPathToExtension(path, ctype)
  127. assert.NoError(t, err)
  128. assert.Contains(t, res, path+"$.htm")
  129. // /space/foo.ttl.acl text/html /space/foo.ttl.acl$.htm
  130. path = "/space/foo.ttl" + config.ACLSuffix
  131. ctype = "text/html"
  132. res, err = MapPathToExtension(path, ctype)
  133. assert.NoError(t, err)
  134. assert.Contains(t, res, path+"$.htm")
  135. // /space/foo.b4r text/html /space/foo.b4r$.htm
  136. path = "/space/foo.bar"
  137. ctype = "text/html"
  138. res, err = MapPathToExtension(path, ctype)
  139. assert.NoError(t, err)
  140. assert.Contains(t, res, path+"$.htm")
  141. }
  142. func TestLookUpCtype(t *testing.T) {
  143. cases := []struct {
  144. in, want string
  145. }{
  146. {".ttl", "text/turtle"},
  147. {".n3", "text/n3"},
  148. {".rdf", "application/rdf+xml"},
  149. {".jsonld", "application/ld+json"},
  150. {".unrecognized_ext", ""},
  151. }
  152. for _, c := range cases {
  153. assert.Equal(t, c.want, LookUpCtype(c.in))
  154. }
  155. }