blob: 11d728cd8093db2ac85f6a57a5f6e54e1c8a6459 [file] [log] [blame]
Philipp Schradercdb5cfc2022-02-20 14:57:07 -08001package requests
2
3import (
4 "bytes"
5 "io"
6 "net/http"
7 "testing"
8
9 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/error_response"
10 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_data_scouting"
11 _ "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_data_scouting_response"
12 "github.com/frc971/971-Robot-Code/scouting/webserver/server"
13 flatbuffers "github.com/google/flatbuffers/go"
14)
15
16// Validates that an unhandled address results in a 404.
17func Test404(t *testing.T) {
18 scoutingServer := server.NewScoutingServer()
19 HandleRequests(scoutingServer)
20 scoutingServer.Start(8080)
21 defer scoutingServer.Stop()
22
23 resp, err := http.Get("http://localhost:8080/requests/foo")
24 if err != nil {
25 t.Fatalf("Failed to get data: %v", err)
26 }
27 if resp.StatusCode != http.StatusNotFound {
28 t.Fatalf("Expected error code 404, but got %d instead", resp.Status)
29 }
30}
31
32// Validates that we can submit new data scouting data.
33func TestSubmitDataScoutingError(t *testing.T) {
34 scoutingServer := server.NewScoutingServer()
35 HandleRequests(scoutingServer)
36 scoutingServer.Start(8080)
37 defer scoutingServer.Stop()
38
39 resp, err := http.Post("http://localhost:8080/requests/submit/data_scouting", "application/octet-stream", bytes.NewReader([]byte("")))
40 if err != nil {
41 t.Fatalf("Failed to send request: %v", err)
42 }
43 if resp.StatusCode != http.StatusBadRequest {
44 t.Fatal("Unexpected status code. Got", resp.Status)
45 }
46
47 responseBytes, err := io.ReadAll(resp.Body)
48 if err != nil {
49 t.Fatal("Failed to read response bytes:", err)
50 }
51 errorResponse := error_response.GetRootAsErrorResponse(responseBytes, 0)
52
53 errorMessage := string(errorResponse.ErrorMessage())
54 if errorMessage != "Failed to parse SubmitDataScouting: runtime error: index out of range [3] with length 0" {
55 t.Fatal("Got mismatched error message:", errorMessage)
56 }
57}
58
59// Validates that we can submit new data scouting data.
60func TestSubmitDataScouting(t *testing.T) {
61 scoutingServer := server.NewScoutingServer()
62 HandleRequests(scoutingServer)
63 scoutingServer.Start(8080)
64 defer scoutingServer.Stop()
65
66 builder := flatbuffers.NewBuilder(1024)
67 builder.Finish((&submit_data_scouting.SubmitDataScoutingT{
68 Team: 971,
69 Match: 1,
70 UpperGoalHits: 9971,
71 }).Pack(builder))
72
73 resp, err := http.Post("http://localhost:8080/requests/submit/data_scouting", "application/octet-stream", bytes.NewReader(builder.FinishedBytes()))
74 if err != nil {
75 t.Fatalf("Failed to send request: %v", err)
76 }
77 if resp.StatusCode != http.StatusNotImplemented {
78 t.Fatal("Unexpected status code. Got", resp.Status)
79 }
80 // TODO(phil): We have nothing to validate yet. Fix that.
81}