Skip to content

Commit

Permalink
fix: regexp for apisix url in get debuginfo api (apache#506)
Browse files Browse the repository at this point in the history
* fix: fix regexp for apisix url in get debuginfo api

* fix: add test case for getRouteWithApisixUrl
  • Loading branch information
liuxiran authored Sep 24, 2020
1 parent f4e42cd commit 69199d2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
13 changes: 6 additions & 7 deletions api/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,16 +523,15 @@ func getRouteWithApisixUrl(c *gin.Context) {
}
result.Name = route.Name
url := conf.BaseUrl
reg, _ := regexp.Compile("(\\d+\\.\\d+\\.\\d+\\.\\d+)")
reg2, _ := regexp.Compile("\\:(\\d+)")
reg, _ := regexp.Compile("://([^/:]+)(:\\d*)?")
res := reg.FindSubmatch([]byte(url))
res2 := reg2.FindSubmatch([]byte(url))
var port string
if len(res2) > 0 {
port = string(res2[0])
var addr string
if len(res) > 0 {
addr = string(res[0])
addr = strings.Replace(addr, "://", "", 1)
}
routeResponse := &service.RouteResponseWithUrl{}
routeResponse.Url = string(res[0]) + port
routeResponse.Url = addr
routeResponse.RouteRequest = *result
resp, _ := json.Marshal(routeResponse)
c.Data(http.StatusOK, service.ContentType, resp)
Expand Down
29 changes: 29 additions & 0 deletions api/route/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,35 @@ func TestOfflineRoute(t *testing.T) {
handler.Put(uriPrefix + "/routes/" + routePublished.ID.String() + "/offline").Expect(t).Status(http.StatusOK).End()
}

func TestGetRouteWithApisixUrl(t *testing.T) {
// create route
handler.Post(uriPrefix+"/routes").
Header("Authorization", token).
JSON(`{
"name":"api-test-get-url",
"desc":"",
"priority":0,
"protocols":["http"],
"hosts":["test.com"],
"paths":["/*"],
"methods":["GET","HEAD","POST","PUT","DELETE","OPTIONS","PATCH"],
"status":true,
"upstream_protocol":"keep",
"plugins":{},
"uris":["/*"],
"vars":[],
"upstream":{"type":"roundrobin","nodes":{"127.0.0.1:443":1},
"timeout":{"connect":6000,"send":6000,"read":6000}},
"upstream_header":{}
}`).Expect(t).
Status(http.StatusOK).
End()
//get route
route, _ := getRouteByName("api-test-get-url")
// get route with apisix url
handler.Get(uriPrefix + "/routes/" + route.ID.String() + "/debuginfo").Expect(t).Status(http.StatusOK).End()
}

func getRouteByName(name string) (*service.Route, error) {
db := conf.DB()
route := &service.Route{}
Expand Down

0 comments on commit 69199d2

Please sign in to comment.