text_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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 TestTextResponse(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. Text(w, r, "Some plain text")
  17. })
  18. handler.ServeHTTP(w, r)
  19. resp := w.Result()
  20. defer resp.Body.Close()
  21. if resp.StatusCode != http.StatusOK {
  22. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, http.StatusOK)
  23. }
  24. if actualBody := w.Body.String(); actualBody != "Some plain text" {
  25. t.Fatalf(`Unexpected body, got %q instead of %q`, actualBody, "Some plain text")
  26. }
  27. if actualContentType := resp.Header.Get("Content-Type"); actualContentType != "text/plain; charset=utf-8" {
  28. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, "text/plain; charset=utf-8")
  29. }
  30. }