(t *testing.T)
| 203 | } |
| 204 | |
| 205 | func TestHandleResponse_JSON(t *testing.T) { |
| 206 | body := []byte(`{"code":0,"msg":"ok","data":{"id":"1"}}`) |
| 207 | resp := newApiResp(body, map[string]string{"Content-Type": "application/json"}) |
| 208 | |
| 209 | var out bytes.Buffer |
| 210 | var errOut bytes.Buffer |
| 211 | err := HandleResponse(resp, ResponseOptions{ |
| 212 | Identity: core.AsBot, |
| 213 | Out: &out, |
| 214 | ErrOut: &errOut, |
| 215 | FileIO: &localfileio.LocalFileIO{}, |
| 216 | }) |
| 217 | if err != nil { |
| 218 | t.Fatalf("HandleResponse failed: %v", err) |
| 219 | } |
| 220 | var got map[string]interface{} |
| 221 | if err := json.Unmarshal(out.Bytes(), &got); err != nil { |
| 222 | t.Fatalf("invalid JSON output: %v\n%s", err, out.String()) |
| 223 | } |
| 224 | if got["ok"] != true { |
| 225 | t.Fatalf("ok = %v, want true; output: %s", got["ok"], out.String()) |
| 226 | } |
| 227 | if got["identity"] != "bot" { |
| 228 | t.Fatalf("identity = %v, want bot; output: %s", got["identity"], out.String()) |
| 229 | } |
| 230 | if _, hasCode := got["code"]; hasCode { |
| 231 | t.Fatalf("success envelope leaked outer code field: %s", out.String()) |
| 232 | } |
| 233 | data, ok := got["data"].(map[string]interface{}) |
| 234 | if !ok { |
| 235 | t.Fatalf("data = %T, want object; output: %s", got["data"], out.String()) |
| 236 | } |
| 237 | if data["id"] != "1" { |
| 238 | t.Fatalf("data.id = %v, want 1; output: %s", data["id"], out.String()) |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | func TestHandleResponse_JSONWithJqUsesSuccessEnvelope(t *testing.T) { |
| 243 | body := []byte(`{"code":0,"msg":"ok","data":{"id":"1"}}`) |
nothing calls this directly
no test coverage detected