golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.73k stars 17.63k forks source link

Feature request: mux implementation in httptest allowing modification of handler #29838

Closed geekodour closed 5 years ago

geekodour commented 5 years ago

Currently the mux.Handle implementation in net/http panics if a handler already exists for pattern.

For a test I was writing, It would be nice if I could change the handler for a pattern.

If this is a feature that can be added, I'll be happy to send a PR.

https://godoc.org/net/http#ServeMux.Handle

bradfitz commented 5 years ago

Your issue title says "in httptest", but you're proposing modifying net/http, right? Not httptest?

Can't you just write two small tests rather than one big test?

Or use subtests? Or re-create the ServeMux?

Or use an http.Handler implementation that does the switcheroo muxing itself?

geekodour commented 5 years ago

@bradfitz I managed to do this with recreating the ServeMux for each test.

Your issue title says "in httptest", but you're proposing modifying net/http, right? Not httptest?

I thought, for net/http's mux, it is desirable that It does panic on duplicate pattern.

So, a separate mux implementation in net/http/httptest which allows changing of handlers for the same pattern.

Otherwise,

We can add another method such as mux.HandleNoPanic to net/http which does not panic on duplicate pattern.

But I don't know if it was necessary, I cannot think of any other usecase other than the original issue I listed above for the test which I was able to solve by recreating the ServeMux

bradfitz commented 5 years ago

Okay, let's close this then. If other needs emerge later we can discuss then.