Beispiel #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)
	}
}
Beispiel #2
0
func SetPort(w http.ResponseWriter, r *http.Request) {

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

	//Accept incoming as Json
	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 port cfg.Port
	err = json.Unmarshal(body, &port)
	if err != nil {
		WriteResponse(w, BadRequestStatusCode, GetErrorStatusJson(CouldNotReadRequestDataStatus, err))
		return
	}

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

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