errors_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package elastic
  2. import (
  3. "bufio"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "testing"
  8. )
  9. func TestResponseError(t *testing.T) {
  10. raw := "HTTP/1.1 404 Not Found\r\n" +
  11. "\r\n" +
  12. `{"error":{"root_cause":[{"type":"index_missing_exception","reason":"no such index","index":"elastic-test"}],"type":"index_missing_exception","reason":"no such index","index":"elastic-test"},"status":404}` + "\r\n"
  13. r := bufio.NewReader(strings.NewReader(raw))
  14. req, err := http.NewRequest("GET", "/", nil)
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. resp, err := http.ReadResponse(r, nil)
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. err = checkResponse(req, resp)
  23. if err == nil {
  24. t.Fatalf("expected error; got: %v", err)
  25. }
  26. // Check for correct error message
  27. expected := fmt.Sprintf("elastic: Error %d (%s): no such index [type=index_missing_exception]", resp.StatusCode, http.StatusText(resp.StatusCode))
  28. got := err.Error()
  29. if got != expected {
  30. t.Fatalf("expected %q; got: %q", expected, got)
  31. }
  32. // Check that error is of type *elastic.Error, which contains additional information
  33. e, ok := err.(*Error)
  34. if !ok {
  35. t.Fatal("expected error to be of type *elastic.Error")
  36. }
  37. if e.Status != resp.StatusCode {
  38. t.Fatalf("expected status code %d; got: %d", resp.StatusCode, e.Status)
  39. }
  40. if e.Details == nil {
  41. t.Fatalf("expected error details; got: %v", e.Details)
  42. }
  43. if got, want := e.Details.Index, "elastic-test"; got != want {
  44. t.Fatalf("expected error details index %q; got: %q", want, got)
  45. }
  46. if got, want := e.Details.Type, "index_missing_exception"; got != want {
  47. t.Fatalf("expected error details type %q; got: %q", want, got)
  48. }
  49. if got, want := e.Details.Reason, "no such index"; got != want {
  50. t.Fatalf("expected error details reason %q; got: %q", want, got)
  51. }
  52. if got, want := len(e.Details.RootCause), 1; got != want {
  53. t.Fatalf("expected %d error details root causes; got: %d", want, got)
  54. }
  55. if got, want := e.Details.RootCause[0].Index, "elastic-test"; got != want {
  56. t.Fatalf("expected root cause index %q; got: %q", want, got)
  57. }
  58. if got, want := e.Details.RootCause[0].Type, "index_missing_exception"; got != want {
  59. t.Fatalf("expected root cause type %q; got: %q", want, got)
  60. }
  61. if got, want := e.Details.RootCause[0].Reason, "no such index"; got != want {
  62. t.Fatalf("expected root cause reason %q; got: %q", want, got)
  63. }
  64. }
  65. func TestResponseErrorHTML(t *testing.T) {
  66. raw := "HTTP/1.1 413 Request Entity Too Large\r\n" +
  67. "\r\n" +
  68. `<html>
  69. <head><title>413 Request Entity Too Large</title></head>
  70. <body bgcolor="white">
  71. <center><h1>413 Request Entity Too Large</h1></center>
  72. <hr><center>nginx/1.6.2</center>
  73. </body>
  74. </html>` + "\r\n"
  75. r := bufio.NewReader(strings.NewReader(raw))
  76. req, err := http.NewRequest("GET", "/", nil)
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. resp, err := http.ReadResponse(r, nil)
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. err = checkResponse(req, resp)
  85. if err == nil {
  86. t.Fatalf("expected error; got: %v", err)
  87. }
  88. // Check for correct error message
  89. expected := fmt.Sprintf("elastic: Error %d (%s)", http.StatusRequestEntityTooLarge, http.StatusText(http.StatusRequestEntityTooLarge))
  90. got := err.Error()
  91. if got != expected {
  92. t.Fatalf("expected %q; got: %q", expected, got)
  93. }
  94. }
  95. func TestResponseErrorWithIgnore(t *testing.T) {
  96. raw := "HTTP/1.1 404 Not Found\r\n" +
  97. "\r\n" +
  98. `{"some":"response"}` + "\r\n"
  99. r := bufio.NewReader(strings.NewReader(raw))
  100. req, err := http.NewRequest("HEAD", "/", nil)
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. resp, err := http.ReadResponse(r, nil)
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. err = checkResponse(req, resp)
  109. if err == nil {
  110. t.Fatalf("expected error; got: %v", err)
  111. }
  112. err = checkResponse(req, resp, 404) // ignore 404 errors
  113. if err != nil {
  114. t.Fatalf("expected no error; got: %v", err)
  115. }
  116. }
  117. func TestIsNotFound(t *testing.T) {
  118. if got, want := IsNotFound(nil), false; got != want {
  119. t.Errorf("expected %v; got: %v", want, got)
  120. }
  121. if got, want := IsNotFound(""), false; got != want {
  122. t.Errorf("expected %v; got: %v", want, got)
  123. }
  124. if got, want := IsNotFound(200), false; got != want {
  125. t.Errorf("expected %v; got: %v", want, got)
  126. }
  127. if got, want := IsNotFound(404), true; got != want {
  128. t.Errorf("expected %v; got: %v", want, got)
  129. }
  130. if got, want := IsNotFound(&Error{Status: 404}), true; got != want {
  131. t.Errorf("expected %v; got: %v", want, got)
  132. }
  133. if got, want := IsNotFound(&Error{Status: 200}), false; got != want {
  134. t.Errorf("expected %v; got: %v", want, got)
  135. }
  136. if got, want := IsNotFound(Error{Status: 404}), true; got != want {
  137. t.Errorf("expected %v; got: %v", want, got)
  138. }
  139. if got, want := IsNotFound(Error{Status: 200}), false; got != want {
  140. t.Errorf("expected %v; got: %v", want, got)
  141. }
  142. if got, want := IsNotFound(&http.Response{StatusCode: 404}), true; got != want {
  143. t.Errorf("expected %v; got: %v", want, got)
  144. }
  145. if got, want := IsNotFound(&http.Response{StatusCode: 200}), false; got != want {
  146. t.Errorf("expected %v; got: %v", want, got)
  147. }
  148. }
  149. func TestIsTimeout(t *testing.T) {
  150. if got, want := IsTimeout(nil), false; got != want {
  151. t.Errorf("expected %v; got: %v", want, got)
  152. }
  153. if got, want := IsTimeout(""), false; got != want {
  154. t.Errorf("expected %v; got: %v", want, got)
  155. }
  156. if got, want := IsTimeout(200), false; got != want {
  157. t.Errorf("expected %v; got: %v", want, got)
  158. }
  159. if got, want := IsTimeout(408), true; got != want {
  160. t.Errorf("expected %v; got: %v", want, got)
  161. }
  162. if got, want := IsTimeout(&Error{Status: 408}), true; got != want {
  163. t.Errorf("expected %v; got: %v", want, got)
  164. }
  165. if got, want := IsTimeout(&Error{Status: 200}), false; got != want {
  166. t.Errorf("expected %v; got: %v", want, got)
  167. }
  168. if got, want := IsTimeout(Error{Status: 408}), true; got != want {
  169. t.Errorf("expected %v; got: %v", want, got)
  170. }
  171. if got, want := IsTimeout(Error{Status: 200}), false; got != want {
  172. t.Errorf("expected %v; got: %v", want, got)
  173. }
  174. if got, want := IsTimeout(&http.Response{StatusCode: 408}), true; got != want {
  175. t.Errorf("expected %v; got: %v", want, got)
  176. }
  177. if got, want := IsTimeout(&http.Response{StatusCode: 200}), false; got != want {
  178. t.Errorf("expected %v; got: %v", want, got)
  179. }
  180. }
  181. func TestIsConflict(t *testing.T) {
  182. if got, want := IsConflict(nil), false; got != want {
  183. t.Errorf("expected %v; got: %v", want, got)
  184. }
  185. if got, want := IsConflict(""), false; got != want {
  186. t.Errorf("expected %v; got: %v", want, got)
  187. }
  188. if got, want := IsConflict(200), false; got != want {
  189. t.Errorf("expected %v; got: %v", want, got)
  190. }
  191. if got, want := IsConflict(http.StatusConflict), true; got != want {
  192. t.Errorf("expected %v; got: %v", want, got)
  193. }
  194. if got, want := IsConflict(&Error{Status: 409}), true; got != want {
  195. t.Errorf("expected %v; got: %v", want, got)
  196. }
  197. if got, want := IsConflict(&Error{Status: 200}), false; got != want {
  198. t.Errorf("expected %v; got: %v", want, got)
  199. }
  200. if got, want := IsConflict(Error{Status: 409}), true; got != want {
  201. t.Errorf("expected %v; got: %v", want, got)
  202. }
  203. if got, want := IsConflict(Error{Status: 200}), false; got != want {
  204. t.Errorf("expected %v; got: %v", want, got)
  205. }
  206. if got, want := IsConflict(&http.Response{StatusCode: 409}), true; got != want {
  207. t.Errorf("expected %v; got: %v", want, got)
  208. }
  209. if got, want := IsConflict(&http.Response{StatusCode: 200}), false; got != want {
  210. t.Errorf("expected %v; got: %v", want, got)
  211. }
  212. }
  213. func TestIsStatusCode(t *testing.T) {
  214. tests := []struct {
  215. Error interface{}
  216. Code int
  217. Want bool
  218. }{
  219. // #0
  220. {
  221. Error: nil,
  222. Code: 200,
  223. Want: false,
  224. },
  225. // #1
  226. {
  227. Error: "",
  228. Code: 200,
  229. Want: false,
  230. },
  231. // #2
  232. {
  233. Error: http.StatusConflict,
  234. Code: 409,
  235. Want: true,
  236. },
  237. // #3
  238. {
  239. Error: http.StatusConflict,
  240. Code: http.StatusInternalServerError,
  241. Want: false,
  242. },
  243. // #4
  244. {
  245. Error: &Error{Status: http.StatusConflict},
  246. Code: 409,
  247. Want: true,
  248. },
  249. // #5
  250. {
  251. Error: Error{Status: http.StatusConflict},
  252. Code: 409,
  253. Want: true,
  254. },
  255. // #6
  256. {
  257. Error: &http.Response{StatusCode: http.StatusConflict},
  258. Code: 409,
  259. Want: true,
  260. },
  261. }
  262. for i, tt := range tests {
  263. if have, want := IsStatusCode(tt.Error, tt.Code), tt.Want; have != want {
  264. t.Errorf("#%d: have %v, want %v", i, have, want)
  265. }
  266. }
  267. }