package dbdoor import ( "encoding/json" "io" "net/http" "strings" "testing" ) func doorResponse(status int, body string) *http.Response { return &http.Response{StatusCode: status, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(body))} } // The marker is additive: the sentence stays what every CLI prints, and only // a 409 with forward 1 and a region carries a Forward. func TestDoorErrorReadsTheForwardMarker(t *testing.T) { t.Parallel() body, _ := json.Marshal(ForwardBody{Error: "shop.example.com keeps its data in region uk.", Forward: 1, Region: "uk", ForwardOps: []string{"query"}}) door := doorError(doorResponse(http.StatusConflict, string(body))).(*DoorError) if door.Message != "shop.example.com keeps its data in region uk." || door.Forward == nil || door.Forward.Region != "uk" || len(door.Forward.Ops) != 1 || door.Forward.Ops[0] != "query" { t.Fatalf("door = %+v %+v", door, door.Forward) } for name, raw := range map[string]string{ "a plain refusal": `{"error":"no"}`, "forward 0": `{"error":"no","forward":0,"region":"uk"}`, "no region": `{"error":"no","forward":1}`, "a region that is not one": `{"error":"no","forward":1,"region":"../x"}`, } { if d := doorError(doorResponse(http.StatusConflict, raw)).(*DoorError); d.Forward != nil { t.Errorf("%s: a marker", name) } } if d := doorError(doorResponse(http.StatusNotFound, string(body))).(*DoorError); d.Forward != nil { t.Error("a 404 carried a marker") } }