client_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package client // import "miniflux.app/http/client"
  4. import (
  5. "testing"
  6. )
  7. func TestClientWithDelay(t *testing.T) {
  8. clt := New("http://httpbin.org/delay/5")
  9. clt.ClientTimeout = 1
  10. _, err := clt.Get()
  11. if err == nil {
  12. t.Fatal(`The client should stops after 1 second`)
  13. }
  14. }
  15. func TestClientWithError(t *testing.T) {
  16. clt := New("http://httpbin.org/status/502")
  17. clt.ClientTimeout = 5
  18. response, err := clt.Get()
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. if response.StatusCode != 502 {
  23. t.Fatalf(`Unexpected response status code: %d`, response.StatusCode)
  24. }
  25. if !response.HasServerFailure() {
  26. t.Fatal(`A 502 error is considered as server failure`)
  27. }
  28. }
  29. func TestClientWithResponseTooLarge(t *testing.T) {
  30. clt := New("http://httpbin.org/bytes/100")
  31. clt.ClientMaxBodySize = 10
  32. _, err := clt.Get()
  33. if err == nil {
  34. t.Fatal(`The client should fails when reading a response too large`)
  35. }
  36. }
  37. func TestClientWithBasicAuth(t *testing.T) {
  38. clt := New("http://httpbin.org/basic-auth/testuser/testpassword")
  39. clt.WithCredentials("testuser", "testpassword")
  40. _, err := clt.Get()
  41. if err != nil {
  42. t.Fatalf(`The client should be authenticated successfully: %v`, err)
  43. }
  44. }