tm_test.go 810 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. )
  10. func serveTMs() *httptest.Server {
  11. tms, err := ioutil.ReadFile("./testFiles/tms.json")
  12. if err != nil {
  13. log.Fatalf("Error reading file: %s", err)
  14. }
  15. f := func(w http.ResponseWriter, r *http.Request) {
  16. w.Header().Set("Content-Type", "application/json")
  17. fmt.Fprintln(w, string(tms))
  18. }
  19. return httptest.NewServer(http.HandlerFunc(f))
  20. }
  21. func TestGetTMs(t *testing.T) {
  22. server := serveTMs()
  23. defer server.Close()
  24. var app Application
  25. app.setBaseURL(server.URL)
  26. tms := app.getTMs("")
  27. if len(tms) != 2 {
  28. t.Fatalf("Not all TMs read! (%d)", len(tms))
  29. }
  30. if tms[0].FriendlyName != "Test TM 1" || tms[1].FriendlyName != "Test TM 2" {
  31. t.Fatalf("Something went wrong while reading TMs!\n%v", tms)
  32. }
  33. }