tm_test.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. }
  34. func TestGetTMsWrongStatus(t *testing.T) {
  35. server := fakeServer(http.StatusBadRequest, "")
  36. defer server.Close()
  37. var app Application
  38. app.setBaseURL(server.URL)
  39. tms := app.getTMs("")
  40. if len(tms) != 0 {
  41. t.Fatal("There should be no TMs!")
  42. }
  43. }