aboutsummaryrefslogtreecommitdiff
path: root/api/departures_test.go
diff options
context:
space:
mode:
authorJulien Dessaux2021-03-18 20:12:43 +0100
committerJulien Dessaux2021-03-19 11:27:21 +0100
commitbb78fe4cbac510b54d49f41efbdb35ffe7175299 (patch)
tree4c5ebba1ac3cae5b5e2b0755e2de1a0f3c00eb8d /api/departures_test.go
parentInitial import (diff)
downloadtrains-bb78fe4cbac510b54d49f41efbdb35ffe7175299.tar.gz
trains-bb78fe4cbac510b54d49f41efbdb35ffe7175299.tar.bz2
trains-bb78fe4cbac510b54d49f41efbdb35ffe7175299.zip
Added api module with departures feature
Diffstat (limited to '')
-rw-r--r--api/departures_test.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/api/departures_test.go b/api/departures_test.go
new file mode 100644
index 0000000..b0a5dc5
--- /dev/null
+++ b/api/departures_test.go
@@ -0,0 +1,48 @@
+package api
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "testing"
+)
+
+func TestGetDepartures(t *testing.T) {
+ // invalid characters in token
+ client := NewClient("}")
+ _, err := client.GetDepartures()
+ if err == nil {
+ t.Fatalf("invalid characters in token should raise an error")
+ }
+ // unreachable server
+ client = NewClient("https://")
+ _, err = client.GetDepartures()
+ if err == nil {
+ t.Fatalf("unreachable server should raise an error")
+ }
+ // invalid json
+ client, ts := NewTestClientFromFilename(t, "invalid.json")
+ defer ts.Close()
+ _, err = client.GetDepartures()
+ if err == nil {
+ t.Fatalf("invalid json should raise an error")
+ }
+ // http error
+ ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusNotFound)
+ }))
+ client = NewTestClient(ts)
+ _, err = client.GetDepartures()
+ if err == nil {
+ t.Fatalf("404 should raise an error")
+ }
+ // normal working request
+ client, ts = NewTestClientFromFilename(t, "normal-crepieux.json")
+ defer ts.Close()
+ departures, err := client.GetDepartures()
+ if err != nil {
+ t.Fatalf("could not get normal-crepieux departures : %s", err)
+ }
+ if len(departures.Departures) != 10 {
+ t.Fatalf("did not decode normal-crepieux departures properly, got %d departures when expected 10", len(departures.Departures))
+ }
+}