xml_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package xml // import "miniflux.app/http/response/xml"
  5. import (
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. )
  10. func TestOKResponse(t *testing.T) {
  11. r, err := http.NewRequest("GET", "/", nil)
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. w := httptest.NewRecorder()
  16. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  17. OK(w, r, "Some XML")
  18. })
  19. handler.ServeHTTP(w, r)
  20. resp := w.Result()
  21. expectedStatusCode := http.StatusOK
  22. if resp.StatusCode != expectedStatusCode {
  23. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
  24. }
  25. expectedBody := `Some XML`
  26. actualBody := w.Body.String()
  27. if actualBody != expectedBody {
  28. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
  29. }
  30. expectedContentType := "text/xml; charset=utf-8"
  31. actualContentType := resp.Header.Get("Content-Type")
  32. if actualContentType != expectedContentType {
  33. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
  34. }
  35. }
  36. func TestAttachmentResponse(t *testing.T) {
  37. r, err := http.NewRequest("GET", "/", nil)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. w := httptest.NewRecorder()
  42. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  43. Attachment(w, r, "file.xml", "Some XML")
  44. })
  45. handler.ServeHTTP(w, r)
  46. resp := w.Result()
  47. expectedStatusCode := http.StatusOK
  48. if resp.StatusCode != expectedStatusCode {
  49. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
  50. }
  51. expectedBody := `Some XML`
  52. actualBody := w.Body.String()
  53. if actualBody != expectedBody {
  54. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
  55. }
  56. headers := map[string]string{
  57. "Content-Type": "text/xml; charset=utf-8",
  58. "Content-Disposition": "attachment; filename=file.xml",
  59. }
  60. for header, expected := range headers {
  61. actual := resp.Header.Get(header)
  62. if actual != expected {
  63. t.Fatalf(`Unexpected header value, got %q instead of %q`, actual, expected)
  64. }
  65. }
  66. }