diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/config/config.go | 10 | ||||
-rw-r--r-- | pkg/config/config_test.go | 15 | ||||
-rw-r--r-- | pkg/config/error.go | 15 | ||||
-rw-r--r-- | pkg/config/error_test.go | 2 | ||||
-rw-r--r-- | pkg/config/test_data/invalid_trainStop.yaml | 4 | ||||
-rw-r--r-- | pkg/config/test_data/minimal.yaml | 1 | ||||
-rw-r--r-- | pkg/config/test_data/minimal_with_hostname.yaml | 1 | ||||
-rw-r--r-- | pkg/navitia_api_client/departures.go | 4 | ||||
-rw-r--r-- | pkg/navitia_api_client/departures_test.go | 14 |
9 files changed, 50 insertions, 16 deletions
diff --git a/pkg/config/config.go b/pkg/config/config.go index f97467a..6e4bba5 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -9,13 +9,17 @@ import ( ) var validToken = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`) +var validTrainStop = regexp.MustCompile(`^[a-zA-Z0-9:_]+$`) type Config struct { // Address is the hostname or ip the web server will listen to Address string `yaml:"address",default:"127.0.0.1"` - Port string `yaml:"port",default:"8080"` + // Port is the tcp port number or service name the web server will listen to + Port string `yaml:"port",default:"8080"` // Token is the sncf api token Token string `yaml:"token"` + // TrainStop is the navitia code of the train stop the webapp will monitor + TrainStop string `yaml:"trainStop"` } func (c *Config) validate() error { @@ -33,6 +37,10 @@ func (c *Config) validate() error { if ok := validToken.MatchString(c.Token); !ok { return newInvalidTokenError(c.Token) } + // TrainStop + if ok := validTrainStop.MatchString(c.TrainStop); !ok { + return newInvalidTrainStopError(c.TrainStop) + } return nil } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 6969191..a4c170b 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -11,16 +11,18 @@ import ( func TestLoadFile(t *testing.T) { // Minimal yaml file minimalConfig := Config{ - Address: "127.0.0.2", - Port: "8082", - Token: "12345678-9abc-def0-1234-56789abcdef0", + Address: "127.0.0.2", + Port: "8082", + Token: "12345678-9abc-def0-1234-56789abcdef0", + TrainStop: "ABCD:test:01", } // Minimal yaml file with hostname resolving minimalConfigWithResolving := Config{ - Address: "localhost", - Port: "www", - Token: "12345678-9abc-def0-1234-56789abcdef0", + Address: "localhost", + Port: "www", + Token: "12345678-9abc-def0-1234-56789abcdef0", + TrainStop: "VWXY_Z:test:90", } // Test cases @@ -36,6 +38,7 @@ func TestLoadFile(t *testing.T) { {"Unresolvable address should fail to load", "test_data/invalid_address_unresolvable.yaml", nil, &InvalidAddressError{}}, {"Invalid port should fail to load", "test_data/invalid_port.yaml", nil, &InvalidPortError{}}, {"Invalid token should fail to load", "test_data/invalid_token.yaml", nil, &InvalidTokenError{}}, + {"Invalid trainStop should fail to load", "test_data/invalid_trainStop.yaml", nil, &InvalidTrainStopError{}}, {"Minimal config", "test_data/minimal.yaml", &minimalConfig, nil}, {"Minimal config with resolving", "test_data/minimal_with_hostname.yaml", &minimalConfigWithResolving, nil}, } diff --git a/pkg/config/error.go b/pkg/config/error.go index c49b6a9..41a323c 100644 --- a/pkg/config/error.go +++ b/pkg/config/error.go @@ -90,3 +90,18 @@ func newInvalidTokenError(token string) error { token: token, } } + +// Invalid trainStop field error +type InvalidTrainStopError struct { + trainStop string +} + +func (e *InvalidTrainStopError) Error() string { + return fmt.Sprintf("Invalid trainStop %s : it must be a string that lookslike \"stop_area:SNCF:87723502\" (make sure to quote the string because of the colon characters)", e.trainStop) +} + +func newInvalidTrainStopError(trainStop string) error { + return &InvalidTrainStopError{ + trainStop: trainStop, + } +} diff --git a/pkg/config/error_test.go b/pkg/config/error_test.go index f9807c1..4a80b05 100644 --- a/pkg/config/error_test.go +++ b/pkg/config/error_test.go @@ -17,4 +17,6 @@ func TestErrorsCoverage(t *testing.T) { _ = invalidPortErr.Unwrap() invalidTokenErr := InvalidTokenError{} _ = invalidTokenErr.Error() + invalidTrainStopErr := InvalidTrainStopError{} + _ = invalidTrainStopErr.Error() } diff --git a/pkg/config/test_data/invalid_trainStop.yaml b/pkg/config/test_data/invalid_trainStop.yaml new file mode 100644 index 0000000..159950d --- /dev/null +++ b/pkg/config/test_data/invalid_trainStop.yaml @@ -0,0 +1,4 @@ +address: 127.0.0.2 +port: 8082 +token: 12345678-9abc-def0-1234-56789abcdef0 +trainStop: = diff --git a/pkg/config/test_data/minimal.yaml b/pkg/config/test_data/minimal.yaml index 2944645..e34db15 100644 --- a/pkg/config/test_data/minimal.yaml +++ b/pkg/config/test_data/minimal.yaml @@ -1,3 +1,4 @@ address: 127.0.0.2 port: 8082 token: 12345678-9abc-def0-1234-56789abcdef0 +trainStop: "ABCD:test:01" diff --git a/pkg/config/test_data/minimal_with_hostname.yaml b/pkg/config/test_data/minimal_with_hostname.yaml index dbede14..116688a 100644 --- a/pkg/config/test_data/minimal_with_hostname.yaml +++ b/pkg/config/test_data/minimal_with_hostname.yaml @@ -1,3 +1,4 @@ address: localhost port: www token: 12345678-9abc-def0-1234-56789abcdef0 +trainStop: "VWXY_Z:test:90" diff --git a/pkg/navitia_api_client/departures.go b/pkg/navitia_api_client/departures.go index 7738940..cd75191 100644 --- a/pkg/navitia_api_client/departures.go +++ b/pkg/navitia_api_client/departures.go @@ -43,8 +43,8 @@ type DeparturesResponse struct { } `json:"context"` } -func (c *Client) GetDepartures() (departures *DeparturesResponse, err error) { - request := fmt.Sprintf("%s/coverage/sncf/stop_areas/stop_area:SNCF:87723502/departures", c.baseURL) +func (c *Client) GetDepartures(trainStop string) (departures *DeparturesResponse, err error) { + request := fmt.Sprintf("%s/coverage/sncf/stop_areas/%s/departures", c.baseURL, trainStop) start := time.Now() c.mutex.Lock() defer c.mutex.Unlock() diff --git a/pkg/navitia_api_client/departures_test.go b/pkg/navitia_api_client/departures_test.go index 14ffb96..e7d771f 100644 --- a/pkg/navitia_api_client/departures_test.go +++ b/pkg/navitia_api_client/departures_test.go @@ -9,20 +9,20 @@ import ( func TestGetDepartures(t *testing.T) { // invalid characters in token client := NewClient("}") - _, err := client.GetDepartures() + _, err := client.GetDepartures("test") if err == nil { - t.Fatalf("invalid characters in token should raise an error") + t.Fatalf("invalid characters in token should raise an error because the url is invalid") } // unreachable server client = NewClient("https://") - _, err = client.GetDepartures() + _, err = client.GetDepartures("test") if err == nil { t.Fatalf("unreachable server should raise an error") } // invalid json client, ts := newTestClientFromFilename(t, "test_data/invalid.json") defer ts.Close() - _, err = client.GetDepartures() + _, err = client.GetDepartures("test") if err == nil { t.Fatalf("invalid json should raise an error") } @@ -31,14 +31,14 @@ func TestGetDepartures(t *testing.T) { w.WriteHeader(http.StatusNotFound) })) client = newTestClient(ts) - _, err = client.GetDepartures() + _, err = client.GetDepartures("test") if err == nil { t.Fatalf("404 should raise an error") } // normal working request client, ts = newTestClientFromFilename(t, "test_data/normal-crepieux.json") defer ts.Close() - departures, err := client.GetDepartures() + departures, err := client.GetDepartures("test") if err != nil { t.Fatalf("could not get normal-crepieux departures : %s", err) } @@ -47,7 +47,7 @@ func TestGetDepartures(t *testing.T) { } // test the cache (assuming the test takes less than 60 seconds (and it really should) it will be accurate) ts.Close() - departures, err = client.GetDepartures() + departures, err = client.GetDepartures("test") if err != nil { t.Fatalf("could not get normal-crepieux departures : %s", err) } |