Skip to content

redagain/go-testrequest

Repository files navigation

Image alt License Build Status Go Reference

Simple helpers for testing http-handlers and other handlers

Install

go get github.com/redagain/go-testrequest

Helpers

  • Test request builder
  • No-op http.ResponseWriter

Usage example

func TestHandler(t *testing.T) {
	type args struct {
		w http.ResponseWriter
		r *http.Request
	}
	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		{
			name: "Success",
			args: args{
				w: testrequest.NopResponseWriter(),
				r: testrequest.Builder().SetJSONFromValue(
					map[string]interface{}{
						"title":   "The Go Programming Language",
						"isbn":    "978-0134190440",
						"authors": []string{"Alan A. A. Donovan", "Brian W. Kernighan"},
					}).
					SetAccept("application/json").
					SetBearerAuth("4mwbMA6zq9Nxf3XzLk9n01MJX57jdjMAdfYCaJu44vEUJVfdVLF9").
					Request(),
			},
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {    

Examples

  • simpleapi - examples of using helpers to test a simple API
  • clientcredentials - example of testing a function to extract client credentials from a http.Request

About

Simple helpers for testing http-handlers and other handlers

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages