aboutsummaryrefslogtreecommitdiff
path: root/pkg/navitia_api_client/client_test.go
blob: d63db9ec5e2549c71ee3bf14be11a4c2b6884d92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package navitia_api_client

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"testing"
)

// package utilities
func newTestClient(ts *httptest.Server) *NavitiaClient {
	return &NavitiaClient{
		baseURL:    fmt.Sprintf(ts.URL),
		httpClient: ts.Client(),
		cache:      make(map[string]cachedResult),
	}
}

func newTestClientFromFilename(t *testing.T, filename string) (*NavitiaClient, *httptest.Server) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		page, err := ioutil.ReadFile(filename)
		if err != nil {
			t.Fatalf("Could not open test file : %s", err)
		}
		w.Write(page)
	}))
	return newTestClient(ts), ts
}

type testClientCase struct {
	urlPath  string
	filename string
}

func newTestClientFromFilenames(t *testing.T, tcs []testClientCase) (*NavitiaClient, *httptest.Server) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		found := false
		for i := 0; i < len(tcs) && !found; i++ {
			if r.URL.Path+"?"+r.URL.RawQuery == tcs[i].urlPath {
				page, err := ioutil.ReadFile(tcs[i].filename)
				if err != nil {
					t.Fatalf("Could not open test file : %s", err)
				}
				w.Write(page)
				found = true
			}
		}
		if !found {
			http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		}
	}))
	return newTestClient(ts), ts
}