warning_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package headers
  2. import (
  3. "net/http"
  4. "strconv"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestWarningText(t *testing.T) {
  10. testCases := []struct {
  11. code int
  12. expect string
  13. }{
  14. {WarningResponseIsStale, "Response is Stale"},
  15. {WarningRevalidationFailed, "Revalidation Failed"},
  16. {WarningDisconnectedOperation, "Disconnected Operation"},
  17. {WarningHeuristicExpiration, "Heuristic Expiration"},
  18. {WarningMiscellaneousWarning, "Miscellaneous Warning"},
  19. {WarningTransformationApplied, "Transformation Applied"},
  20. {WarningMiscellaneousPersistentWarning, "Miscellaneous Persistent Warning"},
  21. {42, ""},
  22. {1489, ""},
  23. }
  24. for _, tc := range testCases {
  25. t.Run(strconv.Itoa(tc.code), func(t *testing.T) {
  26. assert.Equal(t, tc.expect, WarningText(tc.code))
  27. })
  28. }
  29. }
  30. func TestAddWarning(t *testing.T) {
  31. type args struct {
  32. warn int
  33. agent string
  34. reason string
  35. date time.Time
  36. }
  37. testCases := []struct {
  38. name string
  39. args args
  40. expect http.Header
  41. }{
  42. {
  43. name: "code_only",
  44. args: args{warn: WarningResponseIsStale, agent: "", reason: "", date: time.Time{}},
  45. expect: http.Header{
  46. WarningKey: []string{
  47. "110 -",
  48. },
  49. },
  50. },
  51. {
  52. name: "code_agent",
  53. args: args{warn: WarningResponseIsStale, agent: "ololo/trololo", reason: "", date: time.Time{}},
  54. expect: http.Header{
  55. WarningKey: []string{
  56. "110 ololo/trololo",
  57. },
  58. },
  59. },
  60. {
  61. name: "code_agent_reason",
  62. args: args{warn: WarningResponseIsStale, agent: "ololo/trololo", reason: "shimba-boomba", date: time.Time{}},
  63. expect: http.Header{
  64. WarningKey: []string{
  65. `110 ololo/trololo "shimba-boomba"`,
  66. },
  67. },
  68. },
  69. {
  70. name: "code_agent_reason_date",
  71. args: args{
  72. warn: WarningResponseIsStale,
  73. agent: "ololo/trololo",
  74. reason: "shimba-boomba",
  75. date: time.Date(2019, time.January, 14, 10, 50, 43, 0, time.UTC),
  76. },
  77. expect: http.Header{
  78. WarningKey: []string{
  79. `110 ololo/trololo "shimba-boomba" "Mon, 14 Jan 2019 10:50:43 UTC"`,
  80. },
  81. },
  82. },
  83. {
  84. name: "code_reason_date",
  85. args: args{
  86. warn: WarningResponseIsStale,
  87. agent: "",
  88. reason: "shimba-boomba",
  89. date: time.Date(2019, time.January, 14, 10, 50, 43, 0, time.UTC),
  90. },
  91. expect: http.Header{
  92. WarningKey: []string{
  93. `110 - "shimba-boomba" "Mon, 14 Jan 2019 10:50:43 UTC"`,
  94. },
  95. },
  96. },
  97. }
  98. for _, tc := range testCases {
  99. t.Run(tc.name, func(t *testing.T) {
  100. h := http.Header{}
  101. AddWarning(h, tc.args.warn, tc.args.agent, tc.args.reason, tc.args.date)
  102. assert.Equal(t, tc.expect, h)
  103. })
  104. }
  105. }
  106. func TestParseWarnings(t *testing.T) {
  107. testCases := []struct {
  108. name string
  109. h http.Header
  110. expect []WarningHeader
  111. expectErr bool
  112. }{
  113. {
  114. name: "no_warnings",
  115. h: http.Header{},
  116. expect: nil,
  117. expectErr: false,
  118. },
  119. {
  120. name: "single_code_only",
  121. h: http.Header{
  122. WarningKey: []string{
  123. "110",
  124. },
  125. },
  126. expect: []WarningHeader{
  127. {
  128. Code: 110,
  129. Agent: "",
  130. Reason: "",
  131. Date: time.Time{},
  132. },
  133. },
  134. },
  135. {
  136. name: "single_code_and_empty_agent",
  137. h: http.Header{
  138. WarningKey: []string{
  139. "110 -",
  140. },
  141. },
  142. expect: []WarningHeader{
  143. {
  144. Code: 110,
  145. Agent: "-",
  146. Reason: "",
  147. Date: time.Time{},
  148. },
  149. },
  150. },
  151. {
  152. name: "single_code_and_agent",
  153. h: http.Header{
  154. WarningKey: []string{
  155. "110 shimba/boomba",
  156. },
  157. },
  158. expect: []WarningHeader{
  159. {
  160. Code: 110,
  161. Agent: "shimba/boomba",
  162. Reason: "",
  163. Date: time.Time{},
  164. },
  165. },
  166. },
  167. {
  168. name: "single_code_agent_and_reason",
  169. h: http.Header{
  170. WarningKey: []string{
  171. `110 shimba/boomba "looken tooken"`,
  172. },
  173. },
  174. expect: []WarningHeader{
  175. {
  176. Code: 110,
  177. Agent: "shimba/boomba",
  178. Reason: "looken tooken",
  179. Date: time.Time{},
  180. },
  181. },
  182. },
  183. {
  184. name: "single_full",
  185. h: http.Header{
  186. WarningKey: []string{
  187. `110 shimba/boomba "looken tooken" "Mon, 14 Jan 2019 10:50:43 UTC"`,
  188. },
  189. },
  190. expect: []WarningHeader{
  191. {
  192. Code: 110,
  193. Agent: "shimba/boomba",
  194. Reason: "looken tooken",
  195. Date: time.Date(2019, time.January, 14, 10, 50, 43, 0, time.UTC),
  196. },
  197. },
  198. },
  199. {
  200. name: "multiple_full",
  201. h: http.Header{
  202. WarningKey: []string{
  203. `110 shimba/boomba "looken tooken" "Mon, 14 Jan 2019 10:50:43 UTC"`,
  204. `112 chiken "cooken" "Mon, 15 Jan 2019 10:51:43 UTC"`,
  205. },
  206. },
  207. expect: []WarningHeader{
  208. {
  209. Code: 110,
  210. Agent: "shimba/boomba",
  211. Reason: "looken tooken",
  212. Date: time.Date(2019, time.January, 14, 10, 50, 43, 0, time.UTC),
  213. },
  214. {
  215. Code: 112,
  216. Agent: "chiken",
  217. Reason: "cooken",
  218. Date: time.Date(2019, time.January, 15, 10, 51, 43, 0, time.UTC),
  219. },
  220. },
  221. },
  222. }
  223. for _, tc := range testCases {
  224. t.Run(tc.name, func(t *testing.T) {
  225. got, err := ParseWarnings(tc.h)
  226. if tc.expectErr {
  227. assert.Error(t, err)
  228. } else {
  229. assert.NoError(t, err)
  230. }
  231. assert.Equal(t, tc.expect, got)
  232. })
  233. }
  234. }