Exemplo n.º 1
0
func TestGetTrie(t *testing.T) {

	trie := testTrie()
	trieData := testTrieData()

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

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

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

	if statusCode != 200 {

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

	if data != trieData {
		t.Fatalf("Value from get not as expected \n %s \b %s", data, trieData)
	}
}
Exemplo n.º 2
0
func SetTrie(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 trie cfg.Trie
	err = json.Unmarshal(body, &trie)
	if err != nil {
		WriteResponse(w, BadRequestStatusCode, GetErrorStatusJson(CouldNotReadRequestDataStatus, err))
		return
	}

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

	WriteResponse(w, OkStatusCode, GetStatusJson(RequestSuccesfulStatus))
}