xml_test.go 1.9 KB

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