client_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package ifttt
  2. import (
  3. "context"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "goa.design/clue/log"
  9. )
  10. const (
  11. presentEvent = "presence_detected"
  12. absentEvent = "absence_detected"
  13. )
  14. func TestNewClient(t *testing.T) {
  15. t.Run("invalid base URL", func(t *testing.T) {
  16. _, err := NewClient(http.DefaultClient, "%", "key", presentEvent, absentEvent, false)
  17. assert.ErrorContains(t, err, `parse "%": invalid URL escape "%"`)
  18. })
  19. }
  20. func TestClient_Trigger(t *testing.T) {
  21. ctx := log.Context(context.Background(), log.WithDebug())
  22. cases := []struct {
  23. name, key, event, err string
  24. ctx context.Context
  25. present, noDebug bool
  26. handler func(t *testing.T) http.HandlerFunc
  27. }{
  28. {
  29. name: "preset",
  30. key: "key",
  31. ctx: ctx,
  32. present: true,
  33. handler: func(t *testing.T) http.HandlerFunc {
  34. return func(w http.ResponseWriter, r *http.Request) {
  35. assert := assert.New(t)
  36. assert.Equal(http.MethodPost, r.Method)
  37. assert.Equal("/trigger/"+presentEvent+"/with/key/key", r.URL.Path)
  38. }
  39. },
  40. event: presentEvent,
  41. },
  42. {
  43. name: "absent",
  44. key: "key",
  45. ctx: ctx,
  46. present: false,
  47. handler: func(t *testing.T) http.HandlerFunc {
  48. return func(w http.ResponseWriter, r *http.Request) {
  49. assert := assert.New(t)
  50. assert.Equal(http.MethodPost, r.Method)
  51. assert.Equal("/trigger/"+absentEvent+"/with/key/key", r.URL.Path)
  52. }
  53. },
  54. event: absentEvent,
  55. },
  56. {
  57. name: "nil context",
  58. ctx: nil,
  59. key: "key",
  60. handler: func(t *testing.T) http.HandlerFunc {
  61. return func(w http.ResponseWriter, r *http.Request) {}
  62. },
  63. err: "net/http: nil Context",
  64. },
  65. {
  66. name: "closed connection",
  67. ctx: ctx,
  68. key: "key",
  69. handler: func(t *testing.T) http.HandlerFunc {
  70. return func(w http.ResponseWriter, r *http.Request) {
  71. assert := assert.New(t)
  72. hj, ok := w.(http.Hijacker)
  73. if !assert.Equal(true, ok) {
  74. assert.FailNow("server doesn't support hijacking")
  75. }
  76. conn, _, err := hj.Hijack()
  77. if !assert.NoError(err) {
  78. assert.FailNow("error hijacking")
  79. }
  80. conn.Close()
  81. }
  82. },
  83. err: "EOF",
  84. },
  85. {
  86. name: "unauthorized",
  87. ctx: ctx,
  88. key: "key",
  89. handler: func(t *testing.T) http.HandlerFunc {
  90. return func(w http.ResponseWriter, r *http.Request) {
  91. w.WriteHeader(http.StatusUnauthorized)
  92. _, _ = w.Write([]byte(`{"errors":[{"message":"You sent an invalid key."}]}`))
  93. }
  94. },
  95. err: `401 Unauthorized: {"errors":[{"message":"You sent an invalid key."}]}`,
  96. },
  97. {
  98. name: "empty body",
  99. ctx: ctx,
  100. key: "key",
  101. handler: func(t *testing.T) http.HandlerFunc {
  102. return func(w http.ResponseWriter, r *http.Request) {
  103. w.WriteHeader(http.StatusInternalServerError)
  104. }
  105. },
  106. err: "500 Internal Server Error: <empty body>",
  107. },
  108. {
  109. name: "failed to read body",
  110. ctx: ctx,
  111. key: "key",
  112. noDebug: true, // goahttp.DebugDoer interferes with this test
  113. handler: func(t *testing.T) http.HandlerFunc {
  114. return func(w http.ResponseWriter, r *http.Request) {
  115. w.Header().Set("Content-Length", "1")
  116. w.WriteHeader(http.StatusBadGateway)
  117. }
  118. },
  119. err: "502 Bad Gateway: <failed to read body: unexpected EOF>",
  120. },
  121. }
  122. for _, tc := range cases {
  123. tc := tc
  124. t.Run(tc.name, func(t *testing.T) {
  125. t.Parallel()
  126. assert := assert.New(t)
  127. ts := httptest.NewTLSServer(tc.handler(t))
  128. defer ts.Close()
  129. c, err := NewClient(ts.Client(), ts.URL, tc.key, presentEvent, absentEvent, !tc.noDebug)
  130. assert.NoError(err)
  131. event, err := c.Trigger(tc.ctx, tc.present)
  132. if tc.err != "" {
  133. assert.ErrorContains(err, tc.err)
  134. } else {
  135. assert.NoError(err)
  136. assert.Equal(tc.event, event)
  137. }
  138. })
  139. }
  140. }