Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 1 | package requests |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "testing" |
| 8 | |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame^] | 9 | "github.com/frc971/971-Robot-Code/scouting/db" |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 10 | "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/error_response" |
| 11 | "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_data_scouting" |
| 12 | _ "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_data_scouting_response" |
| 13 | "github.com/frc971/971-Robot-Code/scouting/webserver/server" |
| 14 | flatbuffers "github.com/google/flatbuffers/go" |
| 15 | ) |
| 16 | |
| 17 | // Validates that an unhandled address results in a 404. |
| 18 | func Test404(t *testing.T) { |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame^] | 19 | db := MockDatabase{} |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 20 | scoutingServer := server.NewScoutingServer() |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame^] | 21 | HandleRequests(&db, scoutingServer) |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 22 | scoutingServer.Start(8080) |
| 23 | defer scoutingServer.Stop() |
| 24 | |
| 25 | resp, err := http.Get("http://localhost:8080/requests/foo") |
| 26 | if err != nil { |
| 27 | t.Fatalf("Failed to get data: %v", err) |
| 28 | } |
| 29 | if resp.StatusCode != http.StatusNotFound { |
| 30 | t.Fatalf("Expected error code 404, but got %d instead", resp.Status) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // Validates that we can submit new data scouting data. |
| 35 | func TestSubmitDataScoutingError(t *testing.T) { |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame^] | 36 | db := MockDatabase{} |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 37 | scoutingServer := server.NewScoutingServer() |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame^] | 38 | HandleRequests(&db, scoutingServer) |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 39 | scoutingServer.Start(8080) |
| 40 | defer scoutingServer.Stop() |
| 41 | |
| 42 | resp, err := http.Post("http://localhost:8080/requests/submit/data_scouting", "application/octet-stream", bytes.NewReader([]byte(""))) |
| 43 | if err != nil { |
| 44 | t.Fatalf("Failed to send request: %v", err) |
| 45 | } |
| 46 | if resp.StatusCode != http.StatusBadRequest { |
| 47 | t.Fatal("Unexpected status code. Got", resp.Status) |
| 48 | } |
| 49 | |
| 50 | responseBytes, err := io.ReadAll(resp.Body) |
| 51 | if err != nil { |
| 52 | t.Fatal("Failed to read response bytes:", err) |
| 53 | } |
| 54 | errorResponse := error_response.GetRootAsErrorResponse(responseBytes, 0) |
| 55 | |
| 56 | errorMessage := string(errorResponse.ErrorMessage()) |
| 57 | if errorMessage != "Failed to parse SubmitDataScouting: runtime error: index out of range [3] with length 0" { |
| 58 | t.Fatal("Got mismatched error message:", errorMessage) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Validates that we can submit new data scouting data. |
| 63 | func TestSubmitDataScouting(t *testing.T) { |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame^] | 64 | db := MockDatabase{} |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 65 | scoutingServer := server.NewScoutingServer() |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame^] | 66 | HandleRequests(&db, scoutingServer) |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 67 | scoutingServer.Start(8080) |
| 68 | defer scoutingServer.Stop() |
| 69 | |
| 70 | builder := flatbuffers.NewBuilder(1024) |
| 71 | builder.Finish((&submit_data_scouting.SubmitDataScoutingT{ |
| 72 | Team: 971, |
| 73 | Match: 1, |
| 74 | UpperGoalHits: 9971, |
| 75 | }).Pack(builder)) |
| 76 | |
| 77 | resp, err := http.Post("http://localhost:8080/requests/submit/data_scouting", "application/octet-stream", bytes.NewReader(builder.FinishedBytes())) |
| 78 | if err != nil { |
| 79 | t.Fatalf("Failed to send request: %v", err) |
| 80 | } |
| 81 | if resp.StatusCode != http.StatusNotImplemented { |
| 82 | t.Fatal("Unexpected status code. Got", resp.Status) |
| 83 | } |
| 84 | // TODO(phil): We have nothing to validate yet. Fix that. |
| 85 | } |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame^] | 86 | |
| 87 | // A mocked database we can use for testing. Add functionality to this as |
| 88 | // needed for your tests. |
| 89 | |
| 90 | type MockDatabase struct{} |
| 91 | |
| 92 | func (database *MockDatabase) AddToMatch(db.Match) error { |
| 93 | return nil |
| 94 | } |
| 95 | |
| 96 | func (database *MockDatabase) AddToStats(db.Stats) error { |
| 97 | return nil |
| 98 | } |
| 99 | |
| 100 | func (database *MockDatabase) ReturnMatches() ([]db.Match, error) { |
| 101 | return []db.Match{}, nil |
| 102 | } |
| 103 | |
| 104 | func (database *MockDatabase) ReturnStats() ([]db.Stats, error) { |
| 105 | return []db.Stats{}, nil |
| 106 | } |
| 107 | |
| 108 | func (database *MockDatabase) QueryMatches(int) ([]db.Match, error) { |
| 109 | return []db.Match{}, nil |
| 110 | } |
| 111 | |
| 112 | func (database *MockDatabase) QueryStats(int) ([]db.Stats, error) { |
| 113 | return []db.Stats{}, nil |
| 114 | } |