xml_test.go 2.1 KB

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