func TestGetService(t *testing.T) { s := newTestServer("", "", "") defer s.Stop() m := msg.Service{ UUID: "123", Name: "TestService", Version: "1.0.0", Region: "Test", Host: "localhost", Environment: "Production", Port: 9000, TTL: 4, Expires: getExpirationTime(4), } s.registry.Add(m) req, _ := http.NewRequest("GET", "/skydns/services/"+m.UUID, nil) resp := httptest.NewRecorder() s.router.ServeHTTP(resp, req) if resp.Code != http.StatusOK { t.Fatal("Failed to retrieve service") } m.TTL = 3 // TTL will be lower as time has passed expected, err := json.Marshal(m) if err != nil { t.Fatal(err) } // Newline is expected expected = append(expected, []byte("\n")...) if !bytes.Equal(resp.Body.Bytes(), expected) { t.Fatalf("Returned service is invalid. Expected %q but received %q", string(expected), resp.Body.String()) } }
func TestUpdateTTL(t *testing.T) { s := newTestServer("", "", "") defer s.Stop() m := msg.Service{ UUID: "123", Name: "TestService", Version: "1.0.0", Region: "Test", Host: "localhost", Environment: "Production", Port: 9000, TTL: 4, } s.registry.Add(m) m.TTL = 25 b, err := json.Marshal(m) if err != nil { t.Fatal(err) } req, _ := http.NewRequest("PATCH", "/skydns/services/"+m.UUID, bytes.NewBuffer(b)) resp := httptest.NewRecorder() s.router.ServeHTTP(resp, req) if resp.Code != http.StatusOK { t.Fatal("Failed to update TTL") } if serv, err := s.registry.GetUUID(m.UUID); err != nil || serv.TTL != 24 { t.Fatal("Failed to update TTL", err, serv.TTL) } }