client_test.go 1.3 KB

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