func TestHTTPS(t *testing.T) { // Start our recorder r, err := recorder.New("fixtures/iana-reserved-domains") if err != nil { t.Fatal(err) } defer r.Stop() // Make sure recorder is stopped once done with it // Create an HTTP client and inject our transport client := &http.Client{ Transport: r.Transport, // Inject our transport! } url := "https://www.iana.org/domains/reserved" resp, err := client.Get(url) if err != nil { t.Fatalf("Failed to get url %s: %s", url, err) } body, err := ioutil.ReadAll(resp.Body) if err != nil { t.Fatalf("Failed to read response body: %s", err) } wantHeading := "<h1>IANA-managed Reserved Domains</h1>" bodyContent := string(body) if !strings.Contains(bodyContent, wantHeading) { t.Errorf("Heading %s not found in response", wantHeading) } }
func TestSimple(t *testing.T) { // Start our recorder r, err := recorder.New("fixtures/golang-org") if err != nil { t.Fatal(err) } defer r.Stop() // Make sure recorder is stopped once done with it // Create an HTTP client and inject our transport client := &http.Client{ Transport: r.Transport, // Inject our transport! } url := "http://golang.org/" resp, err := client.Get(url) if err != nil { t.Fatalf("Failed to get url %s: %s", url, err) } body, err := ioutil.ReadAll(resp.Body) if err != nil { t.Fatalf("Failed to read response body: %s", err) } wantTitle := "<title>The Go Programming Language</title>" bodyContent := string(body) if !strings.Contains(bodyContent, wantTitle) { t.Errorf("Title %s not found in response", wantTitle) } }
func TestEtcd(t *testing.T) { // Start our recorder r, err := recorder.New("fixtures/etcd") if err != nil { t.Fatal(err) } defer r.Stop() // Make sure recorder is stopped once done with it // Create an etcd configuration using our transport cfg := client.Config{ Endpoints: []string{"http://127.0.0.1:2379"}, HeaderTimeoutPerRequest: time.Second, Transport: r.Transport, // Inject our transport! } // Create an etcd client using the above configuration c, err := client.New(cfg) if err != nil { t.Fatalf("Failed to create etcd client: %s", err) } kapi := client.NewKeysAPI(c) // Etcd key and value we use wantKey := "/foo" wantValue := "bar" // Set the key in etcd _, err = kapi.Set(context.Background(), wantKey, wantValue, nil) if err != nil { t.Fatalf("Failed to set etcd key: %s", err) } // Get the key from etcd resp, err := kapi.Get(context.Background(), wantKey, nil) if err != nil { t.Fatalf("Failed to get etcd key: %s", err) } gotValue := resp.Node.Value if wantValue != gotValue { t.Errorf("want %q value, got %q value", wantValue, gotValue) } }
func TestJiraFetchWithRecording(t *testing.T) { resource.Require(t, resource.UnitTest) r, err := recorder.New("../test/data/jira_fetch_test") if err != nil { t.Error(err) } defer r.Stop() h := &http.Client{ Timeout: 100 * time.Second, Transport: r.Transport, } f := jiraIssueFetcher{} j := JiraTracker{URL: "https://issues.jboss.org", Query: "project = Arquillian AND status = Closed AND assignee = aslak AND fixVersion = 1.1.11.Final AND priority = Major ORDER BY created ASC"} client, _ := jira.NewClient(h, j.URL) f.client = client fetch := j.fetch(&f) i := <-fetch if i.ID != `"ARQ-1937"` { t.Errorf("ID is not matching: %#v", string(i.ID)) } i = <-fetch if i.ID != `"ARQ-1956"` { t.Errorf("ID is not matching: %#v", string(i.ID)) } i = <-fetch if i.ID != `"ARQ-1996"` { t.Errorf("ID is not matching: %#v", string(i.ID)) } i = <-fetch if i.ID != `"ARQ-2009"` { t.Errorf("ID is not matching: %#v", string(i.ID)) } }
// Creates a new etcd client with recording enabled func mustNewTestClient(cassette string) *testClient { // Start our recorder r, err := recorder.New(cassette) if err != nil { panic(err) } cfg := etcdclient.Config{ Endpoints: []string{"http://127.0.0.1:2379"}, Transport: r, // Inject our transport! HeaderTimeoutPerRequest: etcdclient.DefaultRequestTimeout, } klient := client.NewEtcdMinionClient(cfg) tc := &testClient{ client: klient, config: cfg, recorder: r, } return tc }
func TestGithubFetchWithRecording(t *testing.T) { resource.Require(t, resource.UnitTest) r, err := recorder.New("../test/data/github_fetch_test") require.Nil(t, err) defer r.Stop() h := &http.Client{ Timeout: 1 * time.Second, Transport: r.Transport, } f := githubIssueFetcher{} f.client = github.NewClient(h) g := &GithubTracker{URL: "", Query: "is:open is:issue user:almighty-test"} fetch := g.fetch(&f) i := <-fetch if !strings.Contains(string(i.Content), `"html_url":"https://github.com/almighty-test/almighty-test-unit/issues/2"`) { t.Errorf("Content is not matching: %#v", string(i.Content)) } i2 := <-fetch if !strings.Contains(string(i2.Content), `"html_url":"https://github.com/almighty-test/almighty-test-unit/issues/1"`) { t.Errorf("Content is not matching: %#v", string(i2.Content)) } }
func TestRecord(t *testing.T) { runID := time.Now().Format(time.RFC3339Nano) tests := []recordTest{ { method: "GET", out: "GET " + runID, }, { method: "POST", body: strings.NewReader("post body"), out: "POST " + runID + "\npost body", }, } dir, err := ioutil.TempDir("", "") if err != nil { t.Fatal(err) } cassPath := path.Join(dir, "record_test") var serverURL string serverUp := false func() { // Start our recorder r, err := recorder.New(cassPath) if err != nil { t.Fatal(err) } defer r.Stop() // Make sure recorder is stopped once done with it server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%s %s", r.Method, runID) if r.Body != nil { defer r.Body.Close() fmt.Fprintln(w) io.Copy(w, r.Body) } })) serverUp = true defer func() { server.Close() t.Log("server shut down") serverUp = false }() serverURL = server.URL t.Log("recording") for _, test := range tests { test.perform(t, serverURL, r) } }() if serverUp { t.Fatal("expected server to have shut down") } // Re-run without the actual server r, err := recorder.New(cassPath) if err != nil { t.Fatal(err) } defer r.Stop() t.Log("replaying") for _, test := range tests { test.perform(t, serverURL, r) } }