diff --git a/cmd/web/handlers_test.go b/cmd/web/handlers_test.go index 532436e..2f459fe 100644 --- a/cmd/web/handlers_test.go +++ b/cmd/web/handlers_test.go @@ -24,7 +24,7 @@ func TestCreateElection(t *testing.T) { On("Insert", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). Return(1, nil) - path := "/election" + path := "/api/election" tests := []struct { name string @@ -200,7 +200,7 @@ func TestCreateElection_ServerError(t *testing.T) { On("Insert", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). Return(0, fmt.Errorf("")) - path := "/election" + path := "/api/election" requestBody := api.CreateElectionRequest{ Choices: []string{"宮本武蔵", "伊東一刀斎"}, ExpiresAt: time.Now().Add(24 * time.Hour), @@ -254,7 +254,7 @@ func TestCreateVotes_UnknownVotersElection(t *testing.T) { On("Insert", mock.Anything, mock.Anything, mock.Anything, mock.Anything). Return(1, nil) - path := "/election/1/votes" + path := "/api/election/1/votes" tests := []struct { name string @@ -359,7 +359,7 @@ func TestCreateVotes_KnownVotersElection(t *testing.T) { On("Exists", mock.Anything, mock.Anything). Return(false, nil) - path := "/election/1/votes" + path := "/api/election/1/votes" tests := []struct { name string @@ -467,7 +467,7 @@ func TestCreateVotes_NonExistingElection(t *testing.T) { On("GetById", mock.Anything). Return((*models.Election)(nil), sql.ErrNoRows) - path := "/election/1/votes" + path := "/api/election/1/votes" requestBody := api.CreateVotesRequest{ Choices: []struct { ChoiceText string `json:"choiceText"` @@ -498,7 +498,7 @@ func TestCreateVotes_NonNumberElectionID(t *testing.T) { On("GetById", mock.Anything). Return((*models.Election)(nil), sql.ErrNoRows) - path := "/election/1a/votes" + path := "/api/election/1a/votes" requestBody := api.CreateVotesRequest{ Choices: []struct { ChoiceText string `json:"choiceText"` @@ -555,8 +555,8 @@ func TestCreateVotes_AlreadyVoted(t *testing.T) { On("Exists", mock.Anything, mock.Anything). Return(true, nil) - knownVotersElectionPath := "/election/1/votes" - unknownVotersElectionPath := "/election/2/votes" + knownVotersElectionPath := "/api/election/1/votes" + unknownVotersElectionPath := "/api/election/2/votes" voterIdentity := "anything" tests := []struct { @@ -640,7 +640,7 @@ func TestCreateVotes_UnknownVotersElectionMaxVotersReached(t *testing.T) { On("CountByElection", mock.Anything). Return(10, nil) - path := "/election/1/votes" + path := "/api/election/1/votes" requestBody := api.CreateVotesRequest{ Choices: []struct { ChoiceText string `json:"choiceText"` @@ -691,7 +691,7 @@ func TestCreateVotes_ExpiredElection(t *testing.T) { On("CountByElection", mock.Anything). Return(10, nil) - path := "/election/1/votes" + path := "/api/election/1/votes" requestBody := api.CreateVotesRequest{ Choices: []struct { ChoiceText string `json:"choiceText"` diff --git a/cmd/web/routes.go b/cmd/web/routes.go index 150704e..365771f 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -24,6 +24,7 @@ func (app *application) routes() http.Handler { api.HandlerWithOptions(app, api.StdHTTPServerOptions{ BaseRouter: mux, + BaseURL: "/api", ErrorHandlerFunc: app.badRequestError, }) diff --git a/ui/create-election.html b/ui/create-election.html index 96ee915..f6ce862 100644 --- a/ui/create-election.html +++ b/ui/create-election.html @@ -150,7 +150,7 @@ choices: this.election.choices.filter(choice => choice.trim() !== "") // Filter out empty choices }; - fetch("/election", { + fetch("/api/election", { method: "POST", headers: { "Content-Type": "application/json"