tm_test.go 947 B

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