Unit testing #23
| @ -17,3 +17,7 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) { | |||||||
| func (app *application) notImplemented(w http.ResponseWriter, r *http.Request) { | func (app *application) notImplemented(w http.ResponseWriter, r *http.Request) { | ||||||
| 	views.ComingSoon("Coming Soon", app.newCommonData(r)).Render(r.Context(), w) | 	views.ComingSoon("Coming Soon", app.newCommonData(r)).Render(r.Context(), w) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func ping(w http.ResponseWriter, r *http.Request) { | ||||||
|  | 	w.Write([]byte("OK")) | ||||||
|  | } | ||||||
|  | |||||||
							
								
								
									
										19
									
								
								cmd/web/handlers_guestbook_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								cmd/web/handlers_guestbook_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,19 @@ | |||||||
|  | package main | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"net/http" | ||||||
|  | 	"testing" | ||||||
|  | 
 | ||||||
|  | 	"git.32bit.cafe/32bitcafe/guestbook/internal/assert" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func TestPing(t *testing.T) { | ||||||
|  | 	app := newTestApplication(t) | ||||||
|  | 	ts := newTestServer(t, app.routes()) | ||||||
|  | 	defer ts.Close() | ||||||
|  | 
 | ||||||
|  | 	code, _, body := ts.get(t, "/ping") | ||||||
|  | 
 | ||||||
|  | 	assert.Equal(t, code, http.StatusOK) | ||||||
|  | 	assert.Equal(t, body, "OK") | ||||||
|  | } | ||||||
| @ -11,6 +11,8 @@ func (app *application) routes() http.Handler { | |||||||
| 	mux := http.NewServeMux() | 	mux := http.NewServeMux() | ||||||
| 	mux.Handle("GET /static/", http.FileServerFS(ui.Files)) | 	mux.Handle("GET /static/", http.FileServerFS(ui.Files)) | ||||||
| 
 | 
 | ||||||
|  | 	mux.HandleFunc("GET /ping", ping) | ||||||
|  | 
 | ||||||
| 	dynamic := alice.New(app.sessionManager.LoadAndSave, noSurf, app.authenticate) | 	dynamic := alice.New(app.sessionManager.LoadAndSave, noSurf, app.authenticate) | ||||||
| 	standard := alice.New(app.recoverPanic, app.logRequest, commonHeaders) | 	standard := alice.New(app.recoverPanic, app.logRequest, commonHeaders) | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										50
									
								
								cmd/web/testutils_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								cmd/web/testutils_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,50 @@ | |||||||
|  | package main | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"bytes" | ||||||
|  | 	"io" | ||||||
|  | 	"log/slog" | ||||||
|  | 	"net/http" | ||||||
|  | 	"net/http/cookiejar" | ||||||
|  | 	"net/http/httptest" | ||||||
|  | 	"testing" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func newTestApplication(t *testing.T) *application { | ||||||
|  | 	return &application{ | ||||||
|  | 		logger: slog.New(slog.NewTextHandler(io.Discard, nil)), | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type testServer struct { | ||||||
|  | 	*httptest.Server | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func newTestServer(t *testing.T, h http.Handler) *testServer { | ||||||
|  | 	ts := httptest.NewTLSServer(h) | ||||||
|  | 	jar, err := cookiejar.New(nil) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
|  | 	ts.Client().Jar = jar | ||||||
|  | 	ts.Client().CheckRedirect = func(req *http.Request, via []*http.Request) error { | ||||||
|  | 		return http.ErrUseLastResponse | ||||||
|  | 	} | ||||||
|  | 	return &testServer{ts} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (ts *testServer) get(t *testing.T, urlPath string) (int, http.Header, string) { | ||||||
|  | 	rs, err := ts.Client().Get(ts.URL + urlPath) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	defer rs.Body.Close() | ||||||
|  | 	body, err := io.ReadAll(rs.Body) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
|  | 	body = bytes.TrimSpace(body) | ||||||
|  | 
 | ||||||
|  | 	return rs.StatusCode, rs.Header, string(body) | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user