Example #1
0
func TestGetRule(t *testing.T) {

	rule := testRule()
	ruleData := testRuleData()

	if err := zk.SetRule(rule); err != nil {
		t.Fatalf("couldn't set rule for get")
	}

	defer func() {
		if err := zk.DeleteRule(rule.Name); err != nil {
			t.Fatalf("couldn't clean up")
		}
	}()

	statusCode, data, err := client.BuildAndSendRequest("GET", "/rules/"+rule.Name, "")
	if err != nil {
		t.Fatalf("could not get rule: %s", err)
	}

	if statusCode != 200 {

		t.Fatalf("incorrect status code returned, should be 200")
	}

	if data != ruleData {
		t.Fatalf("Value from get not as expected \n %s \b %s", data, ruleData)
	}
}
Example #2
0
func SetRule(w http.ResponseWriter, r *http.Request) {

	err := GetUserSecretAndAuth(r)
	if err != nil {
		WriteResponse(w, NotAuthorizedStatusCode, GetErrorStatusJson(NotAuthenticatedStatus, err))
		return
	}

	if r.Header.Get("Content-Type") != "application/json" {
		WriteResponse(w, BadRequestStatusCode, GetStatusJson(IncorrectContentTypeStatus))
		return
	}

	body, err := GetRequestBody(r)
	if err != nil {
		WriteResponse(w, BadRequestStatusCode, GetErrorStatusJson(CouldNotReadRequestDataStatus, err))
		return
	}

	var rule cfg.Rule
	err = json.Unmarshal(body, &rule)
	if err != nil {
		WriteResponse(w, BadRequestStatusCode, GetErrorStatusJson(CouldNotReadRequestDataStatus, err))
		return
	}

	err = zk.SetRule(rule)
	if err != nil {
		WriteResponse(w, ServerErrorCode, GetErrorStatusJson(CouldNotCompleteOperationStatus, err))
		return
	}

	WriteResponse(w, OkStatusCode, GetStatusJson(RequestSuccesfulStatus))

}