Code Monkey home page Code Monkey logo

mux's Issues

segments after ":name" type will match wrong

there are two conflict router:

GET /v1/team/:tid/duty
GET /v1/team/:svc/info

the secode route will not match

package main

import (
	"log"
	"net/http"

	"github.com/beego/mux"
)

func main() {
	m := mux.New()
	m.Get("/v1/team/:tid/duty", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello, beego mux\n"))
	})
	m.Get("/v1/team/:svc/info", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello, beego mux 2\n"))
	})

	log.Fatal(http.ListenAndServe("127.0.0.1:2222", m))
}

curl:

mozhata@pc:try$ curl http://127.0.0.1:2222/v1/team/ididid/duty
hello, beego mux
mozhata@pc:try$ curl http://127.0.0.1:2222/v1/team/ididid/info
"/v1/team/ididid/info" not implemented

but replace :svc to :tid, these two routes all works

route mapping works wrong when pattern segments has ":name" type

My test enviroment:
os:

mac os x and ubuntu 16.04

go:

$go version
go version go1.8 darwin/amd64

beego/mux version:

commit 38fddcbf1be964bbc26c0cf7b91157719cfdb56f

test code:

package main

import (
    "log"
    "net/http"

    "github.com/beego/mux"
)

func Register(method string, pattern string, handler http.HandlerFunc) {
    mx.Handler(method, pattern, handler)
}

func addServer(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("addServer"))
}

func getServer(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("getServer"))
}

func delServer(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("delServer"))
}

var mx *mux.Mux

func main() {
    mx = mux.New()

    Register("POST", "/api/v1/servers/:server_name", addServer)
    Register("DELETE", "/api/v1/servers/:server_name", delServer)
    Register("GET", "/api/v1/servers/:server_name", getServer)

    log.Fatal(http.ListenAndServe("127.0.0.1:9999", mx))
}

result:

$curl -X POST  http://127.0.0.1:9999/api/v1/servers/yy-pd.com
addServer

$curl http://127.0.0.1:9999/api/v1/servers/yy-pd.com
"GET" not allowed in "/api/v1/servers/yy-pd.com"

$curl -X DELETE  http://127.0.0.1:9999/api/v1/servers/yy-pd.com
"DELETE" not allowed in "/api/v1/servers/yy-pd.com"

expected: all curl command should return right result.

router not work as expected

I add some test code in the test file https://github.com/beego/mux/blob/master/mux_test.go#L21

t.Run("router with wildcard pattern2", func(t *testing.T) {
	assert := assert.New(t)

	mux := New()

	mux.Get("/a/:a/b", func(w http.ResponseWriter, r *http.Request) {
		params := Params(r)
		w.WriteHeader(200)
		w.Write([]byte(params[":a"]))
	})
	mux.Get("/a/:a/b/:b", func(w http.ResponseWriter, r *http.Request) {
		params := Params(r)
		w.WriteHeader(200)
		w.Write([]byte(params[":a"]))
	})
	mux.Get("/a/:a/b/:b/c", func(w http.ResponseWriter, r *http.Request) {
		params := Params(r)
		w.WriteHeader(200)
		w.Write([]byte(params[":a"]))
	})

	ts := httptest.NewServer(mux)
	defer ts.Close()

	res, err := Request("GET", ts.URL+"/a/a1/b/b1/c", nil)
	assert.Nil(err)
	assert.Equal(200, res.StatusCode)
	body, _ := ioutil.ReadAll(res.Body)
	assert.Equal("a1", string(body))
	res.Body.Close()
})

1 .It didn't pass the test, "/a/a1/b/b1/c" not match pattern "/a/:a/b/:b/c"
2. I change the route order, let pattern "/a/:a/b/:b/c" to be first defined router, and it matches, but
"/a/a1/b/b1" not match pattern "/a/:a/b/:b"

It just passes ok in httprouter , is that a bug of mux?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.