client_test.go 1.3 KB

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