From f27047c05ba18329ca182f03af4855e012ac9884 Mon Sep 17 00:00:00 2001 From: opensite Date: Wed, 14 Jul 2021 21:52:44 +0800 Subject: [PATCH] style(transport): remove duplicate get path code (#1188) transport/http remove duplicate get path code for filter --- transport/http/server.go | 5 ----- transport/http/server_test.go | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/transport/http/server.go b/transport/http/server.go index 1c5bc9607..cc314bfa1 100644 --- a/transport/http/server.go +++ b/transport/http/server.go @@ -174,11 +174,6 @@ func (s *Server) filter() mux.MiddlewareFunc { request: req, pathTemplate: pathTemplate, } - if r := mux.CurrentRoute(req); r != nil { - if path, err := r.GetPathTemplate(); err == nil { - tr.operation = path - } - } ctx = transport.NewServerContext(ctx, tr) next.ServeHTTP(w, req.WithContext(ctx)) }) diff --git a/transport/http/server_test.go b/transport/http/server_test.go index 27ae24491..734edc8fb 100644 --- a/transport/http/server_test.go +++ b/transport/http/server_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/go-kratos/kratos/v2/errors" "io/ioutil" "net/http" "strings" @@ -27,6 +28,7 @@ func TestServer(t *testing.T) { ctx := context.Background() srv := NewServer() srv.HandleFunc("/index", fn) + srv.HandleFunc("/index/{id:[0-9]+}", fn) if e, err := srv.Endpoint(); err != nil || e == nil || strings.HasSuffix(e.Host, ":0") { t.Fatal(e, err) @@ -52,6 +54,14 @@ func testClient(t *testing.T, srv *Server) { {"POST", "/index"}, {"PATCH", "/index"}, {"DELETE", "/index"}, + + {"GET", "/index/1"}, + {"PUT", "/index/1"}, + {"POST", "/index/1"}, + {"PATCH", "/index/1"}, + {"DELETE", "/index/1"}, + + {"GET", "/index/notfound"}, } e, err := srv.Endpoint() if err != nil { @@ -69,6 +79,13 @@ func testClient(t *testing.T, srv *Server) { t.Fatal(err) } resp, err := client.Do(req) + + if test.path == "/index/notfound" && err != nil { + if e, ok := err.(*errors.Error); ok && e.Code == http.StatusNotFound { + continue + } + } + if err != nil { t.Fatal(err) } @@ -90,6 +107,11 @@ func testClient(t *testing.T, srv *Server) { for _, test := range tests { var res testData err := client.Invoke(context.Background(), test.method, test.path, nil, &res) + if test.path == "/index/notfound" && err != nil { + if e, ok := err.(*errors.Error); ok && e.Code == http.StatusNotFound { + continue + } + } if err != nil { t.Fatalf("invoke error %v", err) }