mardi 30 juillet 2019

How to serve a raw response using httptest.NewServer

I am having a text file that contains a raw HTTP response like the following

HTTP/1.1 200 OK
date: Tue, 30 Jul 2019 07:47:54 GMT
content-type: application/json

["Hello, world"]

I want to use directly on my mock httptest.Server somehow like in the following pseudocode

f, err := os.Open("testdata/http.response")
if err != nil {
    t.Fatal("Failed to open mapping response mock file")
}

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
     w.WriteRaw(f) 
}))

Right now the best solution I found is to parse the response myself and make the actual w.WriteHeader and w.Write calls manually, but I'm wondering if there is an existing utility that will do this for me

Aucun commentaire:

Enregistrer un commentaire