Skip to content

Commit

Permalink
style(transport): remove duplicate get path code (#1188)
Browse files Browse the repository at this point in the history
transport/http remove duplicate get path code for filter
  • Loading branch information
luckylsx authored Jul 14, 2021
1 parent e8c9a36 commit f27047c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
5 changes: 0 additions & 5 deletions transport/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand Down
22 changes: 22 additions & 0 deletions transport/http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/go-kratos/kratos/v2/errors"
"io/ioutil"
"net/http"
"strings"
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit f27047c

Please sign in to comment.