/
scenario_noBackends.go
39 lines (36 loc) · 1.16 KB
/
scenario_noBackends.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
)
func init() {
scenarios = append(scenarios, RunBadRequestScenario)
}
func RunBadRequestScenario(url, email string, nodes NodeSlice, client *http.Client) (errors []error) {
nodes.Disable()
time.Sleep(10 * time.Second)
resp, err := client.Get(url)
if err != nil {
errors = append(errors, err)
return
}
defer resp.Body.Close()
if resp.StatusCode != 503 {
errors = append(errors, fmt.Errorf("Expected status code 503 when backends are unavailable, but got %d", resp.StatusCode))
}
if contentType := resp.Header.Get("Content-Type"); !strings.HasPrefix(contentType, "application/json") {
errors = append(errors, fmt.Errorf("Expected Content-Type: application/json in response to a bad request, but got %s", contentType))
}
var badRequestResponse errorResponse
err = json.NewDecoder(resp.Body).Decode(&badRequestResponse)
if err != nil {
errors = append(errors, err)
}
if !strings.Contains(badRequestResponse.Error, "no backend nodes available") {
errors = append(errors, fmt.Errorf(`The "error" key in the response didn't contain the string "no backend nodes available"`))
}
return
}