Example #1
0
func TestPoliciesPutV1ParseError(t *testing.T) {
	f := newFixture(t)
	req := newReqV1("PUT", "/policies/test", `
    package a.b.c

    p ;- true
    `)

	f.server.Handler.ServeHTTP(f.recorder, req)

	if f.recorder.Code != 400 {
		t.Errorf("Expected bad request but got %v", f.recorder)
		return
	}

	errs := astErrorV1{}
	if err := util.NewJSONDecoder(f.recorder.Body).Decode(&errs); err != nil {
		t.Fatalf("Unexpected JSON decode error: %v", err)
	}

	expected := ast.NewLocation(nil, "test", 4, 8)

	if !reflect.DeepEqual(errs.Errors[0].Location, expected) {
		t.Errorf("Expected error location to be %v but got: %v", expected, errs)
	}
}
Example #2
0
func TestPoliciesPutV1CompileError(t *testing.T) {
	f := newFixture(t)
	req := newReqV1("PUT", "/policies/test", `
    package a.b.c
    p[x] :- q[x]
    q[x] :- p[x]
    `)

	f.server.Handler.ServeHTTP(f.recorder, req)

	if f.recorder.Code != 400 {
		t.Fatalf("Expected bad request but got %v", f.recorder)
	}

	errs := astErrorV1{}
	if err := util.NewJSONDecoder(f.recorder.Body).Decode(&errs); err != nil {
		t.Fatalf("Unexpected JSON decode error: %v", err)
	}

	expected := ast.NewLocation(nil, "test", 3, 5)

	if len(errs.Errors) != 2 {
		t.Fatalf("Expected exactly two errors but got %d: %v", len(errs.Errors), errs)
	}

	found := false

	for _, err := range errs.Errors {
		if reflect.DeepEqual(err.Location, expected) {
			found = true
		}
	}

	if !found {
		t.Fatalf("Missing expected error %v: %v", expected, errs)
	}
}