> On 18 Aug 2021, at 5:47 am, 'Beginner' via golang-nuts > <golang-nuts@googlegroups.com> wrote: > > Hey, so I am pretty new to Golang and Unit tests as a whole. Right now I am > trying to create some tests for this section of code. I want to test in the > case of an unexpected HTTP response but I don't know how or where to begin. I > am new to mocking and for my previous tests created mocks to test the good > case (with help from my coworker). Can anyone give me advice? What resources > to read that tackle HTTP requests unit testing, etc. I really want to > understand what's going on and don't want to depend on someone 24/7. > > ``` > case http.StatusCreated: > //This part is tested > default: > responseBody, err := ioutil.ReadAll(resp.Body) > if err != nil { > logger.Errorf("Unexpected HTTP response status %v, > unable to read response body", resp.StatusCode) > } else { > logger.Errorf("Unexpected HTTP response status %v, > response body: %s", resp.StatusCode, string(responseBody)) > } > > return "", ErrUnknownStatusCodeResponse > }```
Have a look at https://pkg.go.dev/net/http/httptest#ResponseRecorder <https://pkg.go.dev/net/http/httptest#ResponseRecorder> - I think it should guide you in the right direction. > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com > <mailto:golang-nuts+unsubscr...@googlegroups.com>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/636fa4bd-a3af-40c1-93d5-95255c949fben%40googlegroups.com > > <https://groups.google.com/d/msgid/golang-nuts/636fa4bd-a3af-40c1-93d5-95255c949fben%40googlegroups.com?utm_medium=email&utm_source=footer>. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/0063B8F9-4C96-4B5A-BFDD-4BA87E1977A1%40gmail.com.