Skip to content

BrandonRomano/response

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MPL 2.0 Go Report Card Code Coverage Build Status

Response

Response is a no-frills standardized response body wrapper with some added utility to help manage writing to a http.ResponseWriter.

Output Interface

{
  "status_code": 200,
  "status_text": "OK",
  "error_details": "Invalid email",
  "result": {
    // ...
  }
}

Usage

func MyHandlerFunc(w http.ResponseWriter, r *http.Request) {
    resp := response.New(w)
    defer resp.Output()

    models, err := getModels()
    if err != nil {
        resp.SetResult(http.StatusInternalServerError, nil).
            WithErrorDetails(err.Error())
        return
    }

    resp.SetResult(http.StatusOK, models)
}

Testing

func TestPingSuccess(t *testing.T) {
    recorder := httptest.NewRecorder()
    req := httptest.NewRequest(http.MethodGet, "/ping", nil)
    PingHandlerFunc(recorder, req)

    assert.Equal(t,
        response.Response{
            StatusCode: 200,
            StatusText: "OK",
            Result:     "pong",
        },
        response.Parse(recorder.Result().Body),
    )
}

About

No description, website, or topics provided.

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages