Test TestConfigRouterHasRoutesWellConfigured resolved
This commit is contained in:
@@ -1,5 +1,34 @@
|
|||||||
package data
|
package data
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
)
|
||||||
|
|
||||||
// Rutas a registrar:
|
// Rutas a registrar:
|
||||||
// /handlers/{handler_id}/{resource_path}/request GET
|
// /handlers/{handler_id}/{resource_path}/request GET
|
||||||
// /handlers/{handler_id}/{resource_path}/response PUT
|
// /handlers/{handler_id}/{resource_path}/response PUT
|
||||||
|
|
||||||
|
func configRouter() *mux.Router {
|
||||||
|
r := mux.NewRouter()
|
||||||
|
|
||||||
|
r.HandleFunc("/handlers/{handler_id}/{root}/{resource:.*$}", readResource).Methods("GET")
|
||||||
|
r.HandleFunc("/handlers/{handler_id}/{root}/{resource:.*$}", updateResource).Methods("PUT")
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func Run(bindAddr string) {
|
||||||
|
r := configRouter()
|
||||||
|
|
||||||
|
log.Fatal(http.ListenAndServe(bindAddr, r))
|
||||||
|
}
|
||||||
|
|
||||||
|
func readResource(res http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateResource(res http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,44 +1,49 @@
|
|||||||
package data
|
package data
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestConfigRouterHasRoutesWellConfigured(t *testing.T) {
|
func TestConfigRouterHasRoutesWellConfigured(t *testing.T) {
|
||||||
t.Skip("****** WIP ******")
|
testCases := []struct {
|
||||||
// testCases := []struct {
|
pattern, method string
|
||||||
// pattern, method string
|
handler uintptr
|
||||||
// handler uintptr
|
mustMatch bool
|
||||||
// mustMatch bool
|
vars []struct{ k, v string }
|
||||||
// vars []string
|
}{
|
||||||
// }{
|
{"/handlers/HANDLER_ZZZZZZZZZZZZZZZZ/request/params/name", http.MethodGet, reflect.ValueOf(readResource).Pointer(), true, []struct{ k, v string }{{"handler_id", "HANDLER_ZZZZZZZZZZZZZZZZ"}, {"root", "request"}, {"resource", "params/name"}}},
|
||||||
// {"/handlers/HANDLER_ZZZZZZZZZZZZZZZZ/request/params/name", http.MethodGet, reflect.ValueOf().Pointer(), true, []struct{ key, value string }{{"handler_id", "HANDLER_ZZZZZZZZZZZZZZZZ"}, {"resource", "params/name"}}},
|
{"/handlers/HANDLER_ZZZZZZZZZZZZZZZZ/request/params/name", http.MethodPut, reflect.ValueOf(updateResource).Pointer(), true, []struct{ k, v string }{{"handler_id", "HANDLER_ZZZZZZZZZZZZZZZZ"}, {"root", "request"}, {"resource", "params/name"}}},
|
||||||
// {"/handlers/HANDLER_ZZZZZZZZZZZZZZZZ/request/params/name", http.MethodPut, reflect.ValueOf().Pointer(), true, []struct{ key, value string }{{"handler_id", "HANDLER_ZZZZZZZZZZZZZZZZ"}, {"resource", "params/name"}}},
|
{"/handlers/HANDLER_ZZZZZZZZZZZZZZZZ/response/cookies/name", http.MethodGet, reflect.ValueOf(readResource).Pointer(), true, []struct{ k, v string }{{"handler_id", "HANDLER_ZZZZZZZZZZZZZZZZ"}, {"root", "response"}, {"resource", "cookies/name"}}},
|
||||||
// {"/handlers/HANDLER_ZZZZZZZZZZZZZZZZ/response/cookies/name", http.MethodGet, reflect.ValueOf().Pointer(), true, []struct{ key, value string }{{"handler_id", "HANDLER_ZZZZZZZZZZZZZZZZ"}, {"resource", "cookies/name"}}},
|
{"/handlers/HANDLER_ZZZZZZZZZZZZZZZZ/response/cookies/name", http.MethodPut, reflect.ValueOf(updateResource).Pointer(), true, []struct{ k, v string }{{"handler_id", "HANDLER_ZZZZZZZZZZZZZZZZ"}, {"root", "response"}, {"resource", "cookies/name"}}},
|
||||||
// {"/handlers/HANDLER_ZZZZZZZZZZZZZZZZ/response/cookies/name", http.MethodPut, reflect.ValueOf().Pointer(), true, []struct{ key, value string }{{"handler_id", "HANDLER_ZZZZZZZZZZZZZZZZ"}, {"resource", "cookies/name"}}},
|
}
|
||||||
// }
|
r := configRouter()
|
||||||
// r := configRouter()
|
|
||||||
//
|
for _, tc := range testCases {
|
||||||
// for _, tc := range testCases {
|
rm := mux.RouteMatch{}
|
||||||
// rm := mux.RouteMatch{}
|
rq, _ := http.NewRequest(tc.method, tc.pattern, nil)
|
||||||
// rq, _ := http.NewRequest(tc.method, tc.pattern, nil)
|
if matched := r.Match(rq, &rm); tc.mustMatch == matched {
|
||||||
// if matched := r.Match(rq, &rm); tc.mustMatch == matched {
|
if tc.mustMatch {
|
||||||
// if tc.mustMatch {
|
// Check for Handler match.
|
||||||
// // Check for Handler match.
|
realHandler := reflect.ValueOf(rm.Handler).Pointer()
|
||||||
// realHandler := reflect.ValueOf(rm.Handler).Pointer()
|
if realHandler != tc.handler {
|
||||||
// if realHandler != tc.handler {
|
t.Errorf("Handler mismatch. Expected: %X, got: %X", tc.handler, realHandler)
|
||||||
// t.Errorf("Handler mismatch. Expected: %X, got: %X", tc.handler, realHandler)
|
}
|
||||||
// }
|
|
||||||
//
|
// Check for variables
|
||||||
// // Check for variables
|
for _, v := range tc.vars {
|
||||||
// for _, vn := range tc.vars {
|
if value, exists := rm.Vars[v.k]; !exists {
|
||||||
// if _, exists := rm.Vars[vn]; !exists {
|
t.Errorf("Variable not present: %s", v.k)
|
||||||
// t.Errorf("Variable not present: %s", vn)
|
} else if v.v != value {
|
||||||
// }
|
t.Errorf("Variable value mismatch. Expected: %s, got: %s", v.v, value)
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// } else {
|
}
|
||||||
// t.Errorf("Route mismatch: %+v", tc)
|
} else {
|
||||||
// }
|
t.Errorf("Route mismatch: %+v", tc)
|
||||||
// }
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user