|  |  |  | @ -2,6 +2,7 @@ package main | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | import ( | 
		
	
		
			
				|  |  |  |  | 	"bytes" | 
		
	
		
			
				|  |  |  |  | 	"fmt" | 
		
	
		
			
				|  |  |  |  | 	"io" | 
		
	
		
			
				|  |  |  |  | 	"log/slog" | 
		
	
		
			
				|  |  |  |  | 	"net/http" | 
		
	
	
		
			
				
					|  |  |  | @ -25,8 +26,7 @@ func TestHealthRoute(t *testing.T) { | 
		
	
		
			
				|  |  |  |  | 	} | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 	app := newTestApplication() | 
		
	
		
			
				|  |  |  |  | 	health := http.HandlerFunc(app.healthCheckHandler) | 
		
	
		
			
				|  |  |  |  | 	health.ServeHTTP(respRec, r) | 
		
	
		
			
				|  |  |  |  | 	app.routes().ServeHTTP(respRec, r) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 	resp := respRec.Result() | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
	
		
			
				
					|  |  |  | @ -56,8 +56,7 @@ func TestCreateMovieHandler(t *testing.T) { | 
		
	
		
			
				|  |  |  |  | 	} | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 	app := newTestApplication() | 
		
	
		
			
				|  |  |  |  | 	movie := http.HandlerFunc(app.createMovieHandler) | 
		
	
		
			
				|  |  |  |  | 	movie.ServeHTTP(respRec, r) | 
		
	
		
			
				|  |  |  |  | 	app.routes().ServeHTTP(respRec, r) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 	resp := respRec.Result() | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
	
		
			
				
					|  |  |  | @ -78,32 +77,49 @@ func TestCreateMovieHandler(t *testing.T) { | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | func TestGetAllMoviesHandler(t *testing.T) { | 
		
	
		
			
				|  |  |  |  | 	respRec := httptest.NewRecorder() | 
		
	
		
			
				|  |  |  |  | 	want := "Movies List" | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 	r, err := http.NewRequest(http.MethodGet, "/v1/movies", nil) | 
		
	
		
			
				|  |  |  |  | 	if err != nil { | 
		
	
		
			
				|  |  |  |  | 		t.Fatal(err) | 
		
	
		
			
				|  |  |  |  | 	} | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 	app := newTestApplication() | 
		
	
		
			
				|  |  |  |  | 	movie := http.HandlerFunc(app.getAllMoviesHandler) | 
		
	
		
			
				|  |  |  |  | 	movie.ServeHTTP(respRec, r) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 	resp := respRec.Result() | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 	if resp.StatusCode != http.StatusOK { | 
		
	
		
			
				|  |  |  |  | 		t.Fatalf("Got status code %q, wanted status code %q", resp.StatusCode, http.StatusOK) | 
		
	
		
			
				|  |  |  |  | 	} | 
		
	
		
			
				|  |  |  |  | 	defer resp.Body.Close() | 
		
	
		
			
				|  |  |  |  | 	body, err := io.ReadAll(resp.Body) | 
		
	
		
			
				|  |  |  |  | 	if err != nil { | 
		
	
		
			
				|  |  |  |  | 		t.Fatal(err) | 
		
	
		
			
				|  |  |  |  | 	testTable := []struct { | 
		
	
		
			
				|  |  |  |  | 		name     string | 
		
	
		
			
				|  |  |  |  | 		id       string | 
		
	
		
			
				|  |  |  |  | 		wantCode int | 
		
	
		
			
				|  |  |  |  | 		useID    bool | 
		
	
		
			
				|  |  |  |  | 	}{ | 
		
	
		
			
				|  |  |  |  | 		{ | 
		
	
		
			
				|  |  |  |  | 			name:     "Get Movie By ID", | 
		
	
		
			
				|  |  |  |  | 			id:       "1337", | 
		
	
		
			
				|  |  |  |  | 			wantCode: 200, | 
		
	
		
			
				|  |  |  |  | 		}, | 
		
	
		
			
				|  |  |  |  | 		{ | 
		
	
		
			
				|  |  |  |  | 			name:     "No ID provided", | 
		
	
		
			
				|  |  |  |  | 			id:       "", | 
		
	
		
			
				|  |  |  |  | 			wantCode: 404, | 
		
	
		
			
				|  |  |  |  | 		}, | 
		
	
		
			
				|  |  |  |  | 		{ | 
		
	
		
			
				|  |  |  |  | 			name:     "Negative ID", | 
		
	
		
			
				|  |  |  |  | 			id:       "-1", | 
		
	
		
			
				|  |  |  |  | 			wantCode: 404, | 
		
	
		
			
				|  |  |  |  | 		}, | 
		
	
		
			
				|  |  |  |  | 	} | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 	body = bytes.TrimSpace(body) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 	if !strings.Contains(string(body), want) { | 
		
	
		
			
				|  |  |  |  | 		t.Fatalf("Did not find: %q in response body", want) | 
		
	
		
			
				|  |  |  |  | 	for _, test := range testTable { | 
		
	
		
			
				|  |  |  |  | 		t.Run(test.name, func(t *testing.T) { | 
		
	
		
			
				|  |  |  |  | 			respRec := httptest.NewRecorder() | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 			r, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/v1/movies/%s", test.id), nil) | 
		
	
		
			
				|  |  |  |  | 			t.Logf("Path: %s, Method: %s", r.URL.Path, r.Method) | 
		
	
		
			
				|  |  |  |  | 			if err != nil { | 
		
	
		
			
				|  |  |  |  | 				t.Fatal(err) | 
		
	
		
			
				|  |  |  |  | 			} | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 			app := newTestApplication() | 
		
	
		
			
				|  |  |  |  | 			// want to test with httprouter since we use it to parse context
 | 
		
	
		
			
				|  |  |  |  | 			app.routes().ServeHTTP(respRec, r) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 			resp := respRec.Result() | 
		
	
		
			
				|  |  |  |  | 			t.Logf("Code: %d", resp.StatusCode) | 
		
	
		
			
				|  |  |  |  | 			if resp.StatusCode != test.wantCode { | 
		
	
		
			
				|  |  |  |  | 				t.Fatalf("Got status code '%d', wanted status code '%d'", resp.StatusCode, test.wantCode) | 
		
	
		
			
				|  |  |  |  | 			} | 
		
	
		
			
				|  |  |  |  | 		}) | 
		
	
		
			
				|  |  |  |  | 	} | 
		
	
		
			
				|  |  |  |  | } | 
		
	
	
		
			
				
					|  |  |  | 
 |