| 1 | package dbdoor |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func doorResponse(status int, body string) *http.Response { |
| 12 | return &http.Response{StatusCode: status, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(body))} |
| 13 | } |
| 14 | |
| 15 | // The marker is additive: the sentence stays what every CLI prints, and only |
| 16 | // a 409 with forward 1 and a region carries a Forward. |
| 17 | func TestDoorErrorReadsTheForwardMarker(t *testing.T) { |
| 18 | t.Parallel() |
| 19 | body, _ := json.Marshal(ForwardBody{Error: "shop.example.com keeps its data in region uk.", Forward: 1, Region: "uk", ForwardOps: []string{"query"}}) |
| 20 | door := doorError(doorResponse(http.StatusConflict, string(body))).(*DoorError) |
| 21 | if door.Message != "shop.example.com keeps its data in region uk." || door.Forward == nil || |
| 22 | door.Forward.Region != "uk" || len(door.Forward.Ops) != 1 || door.Forward.Ops[0] != "query" { |
| 23 | t.Fatalf("door = %+v %+v", door, door.Forward) |
| 24 | } |
| 25 | for name, raw := range map[string]string{ |
| 26 | "a plain refusal": `{"error":"no"}`, |
| 27 | "forward 0": `{"error":"no","forward":0,"region":"uk"}`, |
| 28 | "no region": `{"error":"no","forward":1}`, |
| 29 | "a region that is not one": `{"error":"no","forward":1,"region":"../x"}`, |
| 30 | } { |
| 31 | if d := doorError(doorResponse(http.StatusConflict, raw)).(*DoorError); d.Forward != nil { |
| 32 | t.Errorf("%s: a marker", name) |
| 33 | } |
| 34 | } |
| 35 | if d := doorError(doorResponse(http.StatusNotFound, string(body))).(*DoorError); d.Forward != nil { |
| 36 | t.Error("a 404 carried a marker") |
| 37 | } |
| 38 | } |
| 39 | |