Exemplo n.º 1
0
func TestConfigKeyHandler_BadRequest(t *testing.T) {
	ch := make(chan int)
	key := "SIZE"

	conn := NewConn()
	conn.Config = config.NewConfig()
	conn.Config.Set(key, ch)

	input := []struct {
		method  string
		payload io.Reader
	}{
		{"GET", nil},
		{"POST", nil},
		{"POST", bytes.NewBuffer([]byte("{"))},
	}

	for _, in := range input {
		request, err := http.NewRequest(in.method,
			fmt.Sprintf("/_config/%s", key), in.payload)
		if err != nil {
			t.Fatal(err)
		}
		response := httptest.NewRecorder()

		configKeyHandle := conn.configKeyHandler(key)
		configKeyHandle.ServeHTTP(response, request)

		if response.Code != http.StatusBadRequest {
			t.Errorf("response code is %v, expected %v", response.Code, http.StatusBadRequest)
		}
	}
}
Exemplo n.º 2
0
// NewConn creates and returns a new piladb connection.
func NewConn() *Conn {
	conn := &Conn{}
	conn.Pila = pila.NewPila()
	conn.Config = config.NewConfig().Default()
	conn.Status = NewStatus(version.CommitHash(), time.Now(), MemStats())
	return conn
}
Exemplo n.º 3
0
func TestConfigHandler_GET_BadRequest(t *testing.T) {
	ch := make(chan int)

	conn := NewConn()
	conn.Config = config.NewConfig()
	conn.Config.Set("SIZE", ch)

	request, err := http.NewRequest("GET", "/_config", nil)
	if err != nil {
		t.Fatal(err)
	}
	response := httptest.NewRecorder()

	conn.configHandler(response, request)

	if response.Code != http.StatusBadRequest {
		t.Errorf("response code is %v, expected %v", response.Code, http.StatusBadRequest)
	}
}
Exemplo n.º 4
0
func TestConfigKeyHandler_Gone(t *testing.T) {
	key := "no-exist"

	conn := NewConn()
	conn.Config = config.NewConfig()

	request, err := http.NewRequest("GET", fmt.Sprintf("/_config/%s", key), nil)
	if err != nil {
		t.Fatal(err)
	}
	response := httptest.NewRecorder()

	configKeyHandle := conn.configKeyHandler(key)
	configKeyHandle.ServeHTTP(response, request)

	if response.Code != http.StatusGone {
		t.Errorf("response code is %v, expected %v", response.Code, http.StatusGone)
	}
}
Exemplo n.º 5
0
func TestConfigHandler_GET(t *testing.T) {
	conn := NewConn()
	conn.Config = config.NewConfig()
	conn.Config.Set("SIZE", 2)
	conn.Config.Set("PORT", "1205")
	conn.Config.Set("PORT", "8080")

	inputOutput := []struct {
		input, output string
	}{
		{"/_config", `{"stacks":{"PORT":"8080","SIZE":2}}`},
	}

	for _, io := range inputOutput {
		request, err := http.NewRequest("GET", io.input, nil)
		if err != nil {
			t.Fatal(err)
		}
		response := httptest.NewRecorder()

		conn.configHandler(response, request)

		if contentType := response.Header().Get("Content-Type"); contentType != "application/json" {
			t.Errorf("Content-Type is %v, expected %v", contentType, "application/json")
		}

		if response.Code != http.StatusOK {
			t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK)
		}

		config, err := ioutil.ReadAll(response.Body)
		if err != nil {
			t.Fatal(err)
		}

		if string(config) != io.output {
			t.Errorf("config is %s, expected %s", string(config), io.output)
		}
	}
}
Exemplo n.º 6
0
func TestConfigKeyHandler(t *testing.T) {
	conn := NewConn()
	conn.Config = config.NewConfig()
	conn.Config.Set(vars.MaxStackSize, 2)

	element := pila.Element{Value: 2}
	expectedElementJSON, _ := element.ToJSON()

	newElement := pila.Element{Value: 10}
	expectedNewElementJSON, _ := newElement.ToJSON()

	inputOutput := []struct {
		input struct {
			method, key string
			payload     io.Reader
		}
		output struct {
			value    interface{}
			response []byte
		}
	}{
		{struct {
			method, key string
			payload     io.Reader
		}{"GET", vars.MaxStackSize, nil},
			struct {
				value    interface{}
				response []byte
			}{2, expectedElementJSON},
		},
		{struct {
			method, key string
			payload     io.Reader
		}{"POST", vars.MaxStackSize, bytes.NewBuffer(expectedNewElementJSON)},
			struct {
				value    interface{}
				response []byte
			}{10, expectedNewElementJSON},
		},
	}

	for _, io := range inputOutput {
		request, err := http.NewRequest(io.input.method,
			fmt.Sprintf("/_config/%s", io.input.key),
			io.input.payload)
		if err != nil {
			t.Fatal(err)
		}

		response := httptest.NewRecorder()

		configKeyHandle := conn.configKeyHandler(io.input.key)
		configKeyHandle.ServeHTTP(response, request)

		if value := conn.Config.MaxStackSize(); value != io.output.value {
			t.Errorf("value is %d, expected %d", value, io.output.value)
		}

		if contentType := response.Header().Get("Content-Type"); contentType != "application/json" {
			t.Errorf("Content-Type is %v, expected %v", contentType, "application/json")
		}

		if response.Code != http.StatusOK {
			t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK)
		}

		responseJSON, err := ioutil.ReadAll(response.Body)
		if err != nil {
			t.Fatal(err)
		}

		if string(responseJSON) != string(io.output.response) {
			t.Errorf("response is %s, expected %s", string(responseJSON), string(io.output.response))
		}
	}
}