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

	port := testPort()
	portData := testPortData()

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

	defer func() {
		if err := zk.DeletePort(fmt.Sprintf("%d", port.Port)); err != nil {
			t.Fatalf("couldn't clean up")
		}
	}()

	statusCode, data, err := client.BuildAndSendRequest("GET", "/ports/"+fmt.Sprintf("%d", port.Port), "")
	if err != nil {
		t.Fatalf("could not get port: %s", err)
	}

	if statusCode != 200 {

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

	if data != portData {
		t.Fatalf("Value from get not as expected \n %s \b %s", data, portData)
	}
}
Exemplo n.º 2
0
func TestSetPort(t *testing.T) {

	port := testPort()
	portData := testPortData()

	statusCode, data, err := client.BuildAndSendRequest("PUT", "/ports/"+fmt.Sprintf("%d", port.Port), portData)
	if err != nil {
		t.Fatalf("Failed to send request")
	}

	if statusCode != 200 {

		t.Fatalf("Incorrect status code for response")
	}

	defer func() {
		if err := zk.DeletePort(fmt.Sprintf("%d", port.Port)); err != nil {
			t.Fatalf("Couldn't clean up port")
		}
	}()

	statusCode, data, err = client.BuildAndSendRequest("GET", "/ports/"+fmt.Sprintf("%d", port.Port), "")
	if err != nil {
		t.Fatalf("failed to send get request for set verification")
	}

	if statusCode != 200 {
		t.Fatalf("Incorrect status code for get response")
	}

	if data != portData {
		t.Fatalf("Set port failed")
	}
}
Exemplo n.º 3
0
func DeletePort(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

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

	err = zk.DeletePort(vars["Port"])
	if err != nil {
		WriteResponse(w, ServerErrorCode, GetErrorStatusJson(CouldNotCompleteOperationStatus, err))
		return
	}

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