Skip to content

Commit

Permalink
Fixed invalid test name and added validation in TestLongPlay()
Browse files Browse the repository at this point in the history
  • Loading branch information
seborama committed Nov 3, 2018
1 parent b64c9d9 commit 38e6ac5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ deps:
go get -d -t -v ./... $(DEPS)

test: deps
go test -cover -race -parallel 2 ./
go test -cover -race -parallel 2 ./...

examples: deps
go run ./examples/*.go
Expand Down
6 changes: 3 additions & 3 deletions govcr_example3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

const example3CassetteName = "MyCassette3"

func runTestEx4() {
func runTestEx3() {
var samples = []struct {
method string
body string
Expand Down Expand Up @@ -59,9 +59,9 @@ func Example_number3HeaderExclusionVCR() {
govcr.DeleteCassette(example3CassetteName, "")

// 1st run of the test - will use live HTTP calls
runTestEx4()
runTestEx3()
// 2nd run of the test - will use playback
runTestEx4()
runTestEx3()

// Output:
// 404 text/html; charset=UTF-8 true 404 text/html; charset=UTF-8 true 404 text/html; charset=UTF-8 true 404 text/html; charset=UTF-8 true {TracksLoaded:0 TracksRecorded:4 TracksPlayed:0}
Expand Down
43 changes: 43 additions & 0 deletions govcr_example4_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package govcr_test

import (
"fmt"
"io/ioutil"
"strings"

"github.com/seborama/govcr"
)

const example4CassetteName = "MyCassette4"

func runTestEx4() {
// Create vcr and make http call
vcr := govcr.NewVCR(example4CassetteName, nil)
resp, _ := vcr.Client.Get("http://www.example.com/foo")

// Show results
fmt.Printf("%d ", resp.StatusCode)
fmt.Printf("%s ", resp.Header.Get("Content-Type"))

body, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
fmt.Printf("%v ", strings.Contains(string(body), "domain in examples without prior coordination or asking for permission."))

fmt.Printf("%+v\n", vcr.Stats())
}

// Example_simpleVCR is an example use of govcr.
// It shows a simple use of a Long Play cassette (i.e. compressed).
func Example_number4SimpleVCR() {
// Delete cassette to enable live HTTP call
govcr.DeleteCassette(example4CassetteName, "")

// 1st run of the test - will use live HTTP calls
runTestEx4()
// 2nd run of the test - will use playback
runTestEx4()

// Output:
// 404 text/html; charset=UTF-8 true {TracksLoaded:0 TracksRecorded:1 TracksPlayed:0}
// 404 text/html; charset=UTF-8 true {TracksLoaded:1 TracksRecorded:0 TracksPlayed:1}
}
28 changes: 25 additions & 3 deletions govcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestPlaybackOrder(t *testing.T) {
checkStats(t, vcr.Stats(), 0, i, 0)
}

fmt.Println("Phase 2 ================================================")
fmt.Println("Phase 2 - Playback =====================================")
clientNum = 1

// re-run request and expect play back from vcr
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestNonUtf8EncodableBinaryBody(t *testing.T) {
checkStats(t, vcr.Stats(), 0, i, 0)
}

fmt.Println("Phase 2 ================================================")
fmt.Println("Phase 2 - Playback =====================================")
clientNum = 1

// re-run request and expect play back from vcr
Expand Down Expand Up @@ -167,13 +167,35 @@ func TestLongPlay(t *testing.T) {

checkStats(t, vcr.Stats(), 0, i, 0)
}

fmt.Println("Phase 2 - Playback =====================================")
clientNum = 1

// re-run request and expect play back from vcr
vcr = createVCR(cassetteName, false)
client = vcr.Client

// run requests
for i := 1; i <= 10; i++ {
resp, _ := client.Get(ts.URL)

// check outcome of the request
expectedBody := fmt.Sprintf("Hello, client %d", i)
checkResponseForTestPlaybackOrder(t, resp, expectedBody)

if !govcr.CassetteExistsAndValid(cassetteName, "") {
t.Fatalf("CassetteExists: expected true, got false")
}

checkStats(t, vcr.Stats(), 10, 0, i)
}
}

func createVCR(cassetteName string, lp bool) *govcr.VCRControlPanel {
// create a custom http.Transport.
tr := http.DefaultTransport.(*http.Transport)
tr.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true, // just an example, not recommended
InsecureSkipVerify: true, // just an example, strongly discouraged
}

// create a vcr
Expand Down

0 comments on commit 38e6ac5

Please sign in to comment.