blob: 4a19eaff48cc8b801ff4345c4be9c2fc7e5b5afb [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.
Philipp Schraderd9096a32022-02-24 17:53:09 -080085 // TODO(phil): Can we use scouting/webserver/requests/debug here?
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080086}
Philipp Schrader8747f1b2022-02-23 23:56:22 -080087
88// A mocked database we can use for testing. Add functionality to this as
89// needed for your tests.
90
91type MockDatabase struct{}
92
93func (database *MockDatabase) AddToMatch(db.Match) error {
94 return nil
95}
96
97func (database *MockDatabase) AddToStats(db.Stats) error {
98 return nil
99}
100
101func (database *MockDatabase) ReturnMatches() ([]db.Match, error) {
102 return []db.Match{}, nil
103}
104
105func (database *MockDatabase) ReturnStats() ([]db.Stats, error) {
106 return []db.Stats{}, nil
107}
108
109func (database *MockDatabase) QueryMatches(int) ([]db.Match, error) {
110 return []db.Match{}, nil
111}
112
113func (database *MockDatabase) QueryStats(int) ([]db.Stats, error) {
114 return []db.Stats{}, nil
115}