Remove old implentation
Co-authored-by: Hector Hurtado <hector.hurtado@bbva.com>
This commit is contained in:
@@ -1,51 +1 @@
|
|||||||
package data
|
package data
|
||||||
|
|
||||||
import (
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/BBVA/kapow/internal/server/model"
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Rutas a registrar:
|
|
||||||
// /handlers/{handlerId}/response/headers/{item} GET|PUT
|
|
||||||
// /handlers/{handlerId}/response/cookies/{item} GET|PUT
|
|
||||||
// /handlers/{handlerId}/request/headers/{item} GET|PUT
|
|
||||||
// /handlers/{handlerId}/request/cookies/{item} GET|PUT
|
|
||||||
|
|
||||||
var getHandlerId func(string) (*model.Handler, bool) = Handlers.Get
|
|
||||||
|
|
||||||
func updateResource(res http.ResponseWriter, req *http.Request) {
|
|
||||||
vars := mux.Vars(req)
|
|
||||||
hID := vars["handlerId"]
|
|
||||||
|
|
||||||
hnd, ok := getHandlerId(hID)
|
|
||||||
if !ok {
|
|
||||||
res.WriteHeader(http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
resource := vars["resource"]
|
|
||||||
if resource == "response/headers" || resource == "response/cookies" {
|
|
||||||
res.WriteHeader(http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
bodyBytes, err := ioutil.ReadAll(req.Body)
|
|
||||||
if err != nil {
|
|
||||||
res.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Body.Close()
|
|
||||||
value := string(bodyBytes)
|
|
||||||
|
|
||||||
if hnd != nil {
|
|
||||||
hnd.Writing.Lock()
|
|
||||||
hnd.Writer.Header().Add("pepe", value)
|
|
||||||
hnd.Writing.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
res.WriteHeader(http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,129 +1 @@
|
|||||||
package data
|
package data
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/BBVA/kapow/internal/server/model"
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestUpdateResourceNotFoundWhenInvalidHandlerID(t *testing.T) {
|
|
||||||
request := httptest.NewRequest(http.MethodPut, "/handlers/HANDLER_XXXXXXXXXXXXXXXX/response/headers/name", strings.NewReader("value"))
|
|
||||||
response := httptest.NewRecorder()
|
|
||||||
handler := mux.NewRouter()
|
|
||||||
handler.HandleFunc("/handlers/{handlerId}/{resource:.*$}", updateResource).
|
|
||||||
Methods("PUT")
|
|
||||||
|
|
||||||
getHandlerId = func(id string) (*model.Handler, bool) {
|
|
||||||
if id == "HANDLER_YYYYYYYYYYYYYYYY" {
|
|
||||||
return createMockHandler(id, httptest.NewRecorder()), true
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
handler.ServeHTTP(response, request)
|
|
||||||
if response.Code != http.StatusNotFound {
|
|
||||||
t.Errorf("HTTP Status mismatch. Expected: %d, got: %d", http.StatusNotFound, response.Code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestUpdateResourceOkWhenValidHandlerID(t *testing.T) {
|
|
||||||
request := httptest.NewRequest(http.MethodPut, "/handlers/HANDLER_YYYYYYYYYYYY/response/headers/name", strings.NewReader("value"))
|
|
||||||
response := httptest.NewRecorder()
|
|
||||||
handler := mux.NewRouter()
|
|
||||||
handler.HandleFunc("/handlers/{handlerId}/{resource:.*$}", updateResource).
|
|
||||||
Methods("PUT")
|
|
||||||
|
|
||||||
getHandlerId = func(id string) (*model.Handler, bool) {
|
|
||||||
if id == "HANDLER_YYYYYYYYYYYY" {
|
|
||||||
return createMockHandler(id, httptest.NewRecorder()), true
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
handler.ServeHTTP(response, request)
|
|
||||||
if response.Code != http.StatusOK {
|
|
||||||
t.Errorf("HTTP Status mismatch. Expected: %d, got: %d", http.StatusOK, response.Code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestUpdateResourceBadRequestWhenInvalidUrl(t *testing.T) {
|
|
||||||
request := httptest.NewRequest(http.MethodPut, "/handlers/HANDLER_YYYYYYYYYYYYYYYY/response/headers", strings.NewReader("value"))
|
|
||||||
response := httptest.NewRecorder()
|
|
||||||
handler := mux.NewRouter()
|
|
||||||
handler.HandleFunc("/handlers/{handlerId}/{resource:.*$}", updateResource).
|
|
||||||
Methods("PUT")
|
|
||||||
|
|
||||||
getHandlerId = func(id string) (*model.Handler, bool) {
|
|
||||||
if id == "HANDLER_YYYYYYYYYYYYYYYY" {
|
|
||||||
return createMockHandler(id, httptest.NewRecorder()), true
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
handler.ServeHTTP(response, request)
|
|
||||||
if response.Code != http.StatusBadRequest {
|
|
||||||
t.Errorf("HTTP Status mismatch. Expected: %d, got: %d", http.StatusBadRequest, response.Code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestUpdateResourceBadRequestWhenInvalidCookiesUrl(t *testing.T) {
|
|
||||||
request := httptest.NewRequest(http.MethodPut, "/handlers/HANDLER_YYYYYYYYYYYY/response/cookies", strings.NewReader("value"))
|
|
||||||
response := httptest.NewRecorder()
|
|
||||||
handler := mux.NewRouter()
|
|
||||||
handler.HandleFunc("/handlers/{handlerId}/{resource:.*$}", updateResource).
|
|
||||||
Methods("PUT")
|
|
||||||
|
|
||||||
getHandlerId = func(id string) (*model.Handler, bool) {
|
|
||||||
if id == "HANDLER_YYYYYYYYYYYY" {
|
|
||||||
return createMockHandler(id, httptest.NewRecorder()), true
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
handler.ServeHTTP(response, request)
|
|
||||||
if response.Code != http.StatusBadRequest {
|
|
||||||
t.Errorf("HTTP Status mismatch. Expected: %d, got: %d", http.StatusBadRequest, response.Code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestUpdateResourceAddHeaderWhenRecieved(t *testing.T) {
|
|
||||||
request := httptest.NewRequest(http.MethodPut, "/handlers/HANDLER_YYYYYYYYYYYY/response/headers/pepe", strings.NewReader("mola"))
|
|
||||||
response := httptest.NewRecorder()
|
|
||||||
handler := mux.NewRouter()
|
|
||||||
handler.HandleFunc("/handlers/{handlerId}/{resource:.*$}", updateResource).
|
|
||||||
Methods("PUT")
|
|
||||||
|
|
||||||
handlerInResponse := httptest.NewRecorder()
|
|
||||||
getHandlerId = func(id string) (*model.Handler, bool) {
|
|
||||||
if id == "HANDLER_YYYYYYYYYYYY" {
|
|
||||||
return createMockHandler(id, handlerInResponse), true
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
handler.ServeHTTP(response, request)
|
|
||||||
if response.Code != http.StatusOK {
|
|
||||||
t.Errorf("HTTP Status mismatch. Expected: %d, got: %d", http.StatusOK, response.Code)
|
|
||||||
}
|
|
||||||
|
|
||||||
headerValue := handlerInResponse.Result().Header.Get("pepe")
|
|
||||||
if headerValue != "mola" {
|
|
||||||
t.Errorf("Invalid Header value. Expected: %s, got: %s", "mola", headerValue)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func createMockHandler(id string, writer http.ResponseWriter) *model.Handler {
|
|
||||||
return &model.Handler{
|
|
||||||
ID: id,
|
|
||||||
Writer: writer,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user