Re: [go-nuts] ResponseRecorder HTTP status code defaults to 200

2023-10-17 Thread 'Axel Wagner' via golang-nuts
It is not "meant for tests internal to the http package". It's meant for tests of users of the `net/http` package. That is, implementations of the `http.Handler` interface. In the context of the standard library, that is what "general purpose HTTP testing" means. On Tue, Oct 17, 2023 at 10:44 AM S

Re: [go-nuts] ResponseRecorder HTTP status code defaults to 200

2023-10-17 Thread Simon Walter
And so: https://github.com/golang/go/issues/63589 On Tuesday, October 17, 2023 at 10:44:04 AM UTC+2 Simon Walter wrote: > It is good practice to use test lang/tools/libs/etc different from what > you are using in your actual code. > > If the author has meant for the httptest package as general p

Re: [go-nuts] ResponseRecorder HTTP status code defaults to 200

2023-10-17 Thread Simon Walter
It is good practice to use test lang/tools/libs/etc different from what you are using in your actual code. If the author has meant for the httptest package as general purpose HTTP testing library, then, I will argue that such defaults are incorrect. It is incorrect even if the intended purpose

Re: [go-nuts] ResponseRecorder HTTP status code defaults to 200

2023-10-16 Thread 'Axel Wagner' via golang-nuts
To be clear: The behavior of an `http.ResponseWriter` to implicitly use a 200 status code if no explicit WriteHeader call is made is documented: https://pkg.go.dev/net/http#ResponseWriter.WriteHeader I really think it is a bad (or at least strange) idea to claim to implement `http.ResponseWriter` w

Re: [go-nuts] ResponseRecorder HTTP status code defaults to 200

2023-10-16 Thread 'Axel Wagner' via golang-nuts
It seems to me that the fact that the functions accept and return types from `net/http` (like `http.ResponseWriter` and `http.Handler`) and given that it's nested below `net/http` should make it fairly obvious that it's meant to be used with `net/http`. I also genuinely don't understand what the in

Re: [go-nuts] ResponseRecorder HTTP status code defaults to 200

2023-10-16 Thread Simon Walter
Axel, thanks for providing some context. I suppose it is better for me to think of the httptest package as specific to the http package - although this is not explicitly stated: "Package httptest provides utilities for HTTP testing" This seems misleading as is the case with this '200' default.

Re: [go-nuts] ResponseRecorder HTTP status code defaults to 200

2023-10-15 Thread 'Axel Wagner' via golang-nuts
If you want to argue that `net/http` should not set the response code to 200 by default, I would be inclined to agree. I don't particularly like that the API even *allows* you not to explicitly set a response code. But it does. And while it does, the best test is one that matches the behavior of th

Re: [go-nuts] ResponseRecorder HTTP status code defaults to 200

2023-10-15 Thread Simon Walter
How does that explain why it is a good idea? Perhaps my concern is not clear enough. For example: n := 5 funcThatShouldSetNtoFive(&n) if n != 5 { panic("n is not 5) } This is not a good test. I can check if the value has changed by: n := 0 funcThatShouldSetNtoFive(&n) if n != 5 { panic("n

Re: [go-nuts] ResponseRecorder HTTP status code defaults to 200

2023-10-15 Thread 'Axel Wagner' via golang-nuts
A default `net/http` server also uses 200 as the default response code. The behavior of an `http.Handler` not setting a response code should be the same, if it uses `httptest`, as if it uses `net/http`. On Sun, Oct 15, 2023 at 5:17 PM Simon Walter wrote: > Hi all, > > What is the reason that Res

[go-nuts] ResponseRecorder HTTP status code defaults to 200

2023-10-15 Thread Simon Walter
Hi all, What is the reason that ResponseRecorder HTTP status code defaults to 200? https://cs.opensource.google/go/go/+/refs/tags/go1.21.3:src/net/http/httptest/recorder.go;drc=ff14e844d26090e09aa335d836f737c09a7a0402;l=55 https://cs.opensource.google/go/go/+/refs/tags/go1.21.3:src/net/http/http