blob: 6f62ce38916924b7ac34c2f641bf9a5a08acbf25 [file] [log] [blame]
Philipp Schradercdb5cfc2022-02-20 14:57:07 -08001package requests
2
3import (
4 "bytes"
5 "io"
6 "net/http"
7 "testing"
8
Philipp Schrader8747f1b2022-02-23 23:56:22 -08009 "github.com/frc971/971-Robot-Code/scouting/db"
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080010 "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.
18func Test404(t *testing.T) {
Philipp Schrader8747f1b2022-02-23 23:56:22 -080019 db := MockDatabase{}
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080020 scoutingServer := server.NewScoutingServer()
Philipp Schrader8747f1b2022-02-23 23:56:22 -080021 HandleRequests(&db, scoutingServer)
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080022 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.
35func TestSubmitDataScoutingError(t *testing.T) {
Philipp Schrader8747f1b2022-02-23 23:56:22 -080036 db := MockDatabase{}
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080037 scoutingServer := server.NewScoutingServer()
Philipp Schrader8747f1b2022-02-23 23:56:22 -080038 HandleRequests(&db, scoutingServer)
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080039 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.
63func TestSubmitDataScouting(t *testing.T) {
Philipp Schrader8747f1b2022-02-23 23:56:22 -080064 db := MockDatabase{}
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080065 scoutingServer := server.NewScoutingServer()
Philipp Schrader8747f1b2022-02-23 23:56:22 -080066 HandleRequests(&db, scoutingServer)
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080067 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 Schrader8747f1b2022-02-23 23:56:22 -080086
87// A mocked database we can use for testing. Add functionality to this as
88// needed for your tests.
89
90type MockDatabase struct{}
91
92func (database *MockDatabase) AddToMatch(db.Match) error {
93 return nil
94}
95
96func (database *MockDatabase) AddToStats(db.Stats) error {
97 return nil
98}
99
100func (database *MockDatabase) ReturnMatches() ([]db.Match, error) {
101 return []db.Match{}, nil
102}
103
104func (database *MockDatabase) ReturnStats() ([]db.Stats, error) {
105 return []db.Stats{}, nil
106}
107
108func (database *MockDatabase) QueryMatches(int) ([]db.Match, error) {
109 return []db.Match{}, nil
110}
111
112func (database *MockDatabase) QueryStats(int) ([]db.Stats, error) {
113 return []db.Stats{}, nil
114}