-
-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
create tests to cover when client closes request #13
Comments
let me try to work on this. any idea how can we simulate a |
Hi @asasmoyo, thanks. Regarding the tests, probably with something like this:
|
i just tried to use timeout on client. but i got timeout error instead, with nil response. so i can't assert the returned status code |
And on the server side, you got a Check the other tests to get a better idea: https://github.com/nbari/violetear/blob/master/responsewriter_test.go#L161 |
ah wait, i think i am missing something |
my bad, what i did was checking the response status on the client side. since it terminates the connection before server could reply something so it is make sense if resp is nil. anyway, when i make the client to be timeout, the logger here https://github.com/nbari/violetear/blob/master/violetear.go#L264 does not get executed because |
No, first run the tests |
@asasmoyo I came out with this: package main
import (
"fmt"
"net/http"
"net/http/httptest"
"time"
"github.com/nbari/violetear"
)
func main() {
router := violetear.New()
router.Verbose = false
router.LogRequests = true
router.Logger = func(w *violetear.ResponseWriter, r *http.Request) {
fmt.Printf("%d\n", w.Status())
}
router.HandleFunc("*", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(10 * time.Millisecond)
w.Write([]byte("hello world"))
})
ts := httptest.NewServer(router)
defer ts.Close()
client := ts.Client()
client.Timeout = time.Duration(time.Millisecond)
client.Get(ts.URL)
} Give a try it should not panic, and just print a 499 |
great! i was thinking to create the test like this func TestResponseWriterLogger499(t *testing.T) {
var statusChan = make(chan int)
router := New()
router.Verbose = false
router.LogRequests = true
router.Logger = func(w *ResponseWriter, r *http.Request) {
statusChan <- w.Status()
}
router.HandleFunc("*", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(10 * time.Millisecond)
}, "GET")
server := httptest.NewServer(router)
defer server.Close()
client := server.Client()
client.Timeout = time.Millisecond
client.Get(server.URL)
code := <-statusChan
expect(t, code, 499)
} we can capture the returned status code using logger, but race condition may happens |
Give a try to this: https://github.com/nbari/violetear/blob/master/responsewriter_test.go#L161-L177 |
ah how stupid i am :D my bad, i have deleted the code which cause triggering panic. i believe i made a custom request with a cancel context in it. then canceling the request right after it is fired causing panic in https://github.com/nbari/violetear/blob/master/violetear.go#L260 and fires the panic handler. so logger does not get executed |
That test covers only when using a custom |
sure, i'll see if i can do something |
If a client close the request and the router is configured to log requests the returned status is
499
, like Nginx is doing it, for example:This is the output of some requests:
Notice the last entry, it returned a
499
, in this case, the user closed the request(ctrl + c)
after doingcurl 0:8080
This is working but the tests are missing specific to cover this block https://github.com/nbari/violetear/blob/master/responsewriter.go#L24-L30
The text was updated successfully, but these errors were encountered: