PageRenderTime 229ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/pkg/template/generator/examples/remotevalue_test.go

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 36 lines | 29 code | 6 blank | 1 comment | 7 complexity | bf5f97b1db7e801dd41299fa71a2fa3d MD5 | raw file
  1. package examples
  2. import (
  3. "fmt"
  4. "net"
  5. "net/http"
  6. "testing"
  7. )
  8. func TestRemoteValueGenerator(t *testing.T) {
  9. generator := NewRemoteValueGenerator()
  10. _, err := generator.GenerateValue("[GET:http://api.example.com/new]")
  11. if err == nil {
  12. t.Errorf("Expected error while fetching non-existent remote.")
  13. }
  14. }
  15. func TestFakeRemoteValueGenerator(t *testing.T) {
  16. // Run the fake remote server
  17. http.HandleFunc("/v1/value/generate", func(w http.ResponseWriter, r *http.Request) {
  18. fmt.Fprintf(w, "NewRandomString")
  19. })
  20. listener, _ := net.Listen("tcp", ":12345")
  21. go http.Serve(listener, nil)
  22. generator := NewRemoteValueGenerator()
  23. value, err := generator.GenerateValue("[GET:http://127.0.0.1:12345/v1/value/generate]")
  24. if err != nil {
  25. t.Errorf(err.Error())
  26. }
  27. if value != "NewRandomString" {
  28. t.Errorf("Failed to fetch remote value using GET.")
  29. }
  30. }