// this is panicing in goconvey?
func TestErrorReportingOfEmptyJSONArrays(t *testing.T) {
	y := `
---
- name: Testing whitespace sensitivity
  request:
    uri: /hello
    method: POST
    body: '{"email":"*****@*****.**","password":"******"}'
  response:
    code: 200
    body: '{"stuff": [{"foo":"bar"}]}'
`

	fakeEndPoints, err := mockingjay.NewFakeEndpoints([]byte(y))

	if err != nil {
		t.Fatal(err)
	}

	checker := NewCompatabilityChecker()
	realServer := makeFakeDownstreamServer(`{"stuff":[]}`, noSleep)

	if checker.CheckCompatability(fakeEndPoints, realServer.URL) {
		t.Error("Checker shouldn't have found it compatible because its got an empty array downstream")
	}
}
func TestItHandlesBadURLsInConfig(t *testing.T) {
	yaml := fmt.Sprintf(yamlFormat, "not a real url", "foobar")
	fakeEndPoints, _ := mockingjay.NewFakeEndpoints([]byte(yaml))

	if checker.CheckCompatability(fakeEndPoints, "also not a real url") {
		t.Error("Checker should've found that the URL in the YAML cannot be made into a request")
	}
}
func testMockingJayConfig() []mockingjay.FakeEndpoint {

	m, err := mockingjay.NewFakeEndpoints([]byte(testYAML("hello, world")))

	if err != nil {
		log.Fatal(err)
	}

	return m
}
func init() {
	endpoints, err := mockingjay.NewFakeEndpoints([]byte(yaml))

	if err != nil {
		log.Fatal("Couldn't make endpoints for test", endpoints)
	}

	server := mockingjay.NewServer(endpoints)
	http.Handle("/", server)
	go http.ListenAndServe(":9094", nil)
}
// 503439778 ns with 3 endpoints
// 522222809 ns with 100 endpoints (surely we will never see a config that big)
func BenchmarkCompatabilityChecking(b *testing.B) {
	body := "hello, world"
	realServer := makeFakeDownstreamServer(body, sleepyTime)
	checker := NewCompatabilityChecker()
	endpoints, err := mockingjay.NewFakeEndpoints([]byte(multipleEndpointYAML(numberOfEndpoints)))

	if err != nil {
		b.Fatalf("Unable to create checker from YAML %v", err)
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		checker.CheckCompatability(endpoints, realServer.URL)
	}
}
func TestItLaunchesServersAndIsCompatibleWithItsOwnConfig(t *testing.T) {
	endpoints, err := mockingjay.NewFakeEndpoints([]byte(yaml))

	if err != nil {
		t.Log(err)
		t.Fatal("Couldnt make endpoints from YAML")
	}

	server := mockingjay.NewServer(endpoints)
	http.Handle("/", server)
	go http.ListenAndServe(":9094", nil)

	checker := NewCompatabilityChecker()

	if !checker.CheckCompatability(endpoints, "http://localhost:9094") {
		t.Log("Endpoints were not seen as compatible and they should've been.")
		t.Fail()
	}
}
// https://github.com/quii/mockingjay-server/issues/3
func TestWhitespaceSensitivity(t *testing.T) {
	y := `
---
- name: Testing whitespace sensitivity
  request:
    uri: /hello
    method: POST
    body: '{"email":"*****@*****.**","password":"******"}'
  response:
    code: 200
    body: '{"token": "1234abc"}'
        `
	fakeEndPoints, _ := mockingjay.NewFakeEndpoints([]byte(y))
	realServer := makeFakeDownstreamServer(`{"token":    "1234abc"}`, noSleep)

	if !checker.CheckCompatability(fakeEndPoints, realServer.URL) {
		t.Error("Checker should've found that the two JSONs are compatible despite having different whitespace")
	}
}
func TestItIgnoresEndpointsNotSetToCDC(t *testing.T) {
	yaml := `
---
 - name: Endpoint doesnt matter so much as its ignored
   cdcdisabled: true
   request:
     uri: /hello
     method: GET
   response:
     code: 200
     body: 'ok'
`
	endpoints, _ := mockingjay.NewFakeEndpoints([]byte(yaml))

	realServerThatsNotCompatible := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, ":(")
	}))

	if !checker.CheckCompatability(endpoints, realServerThatsNotCompatible.URL) {
		t.Error(`Checker shouldve found downstream server to be "compatible" as the endpoint is ignored`)
	}
}
func TestItMatchesHeaders(t *testing.T) {
	yaml := `
---
 - name: Endpoint with response headers
   request:
     uri: /hello
     method: GET
   response:
     code: 200
     body: 'ok'
     headers:
       content-type: text/json
`
	checker := NewCompatabilityChecker()

	endpoints, err := mockingjay.NewFakeEndpoints([]byte(yaml))

	if err != nil {
		t.Fatal(err)
	}

	realServerWithoutHeaders := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "ok")
	}))

	realServerWithHeaders := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-TYPE", "text/json")
		fmt.Fprint(w, "ok")
	}))

	if checker.CheckCompatability(endpoints, realServerWithoutHeaders.URL) {
		t.Error("Checker should've found downstream server to be incompatible as it did not include the response headers we expected")
	}

	if !checker.CheckCompatability(endpoints, realServerWithHeaders.URL) {
		t.Error("Checker shouldve found downstream server to be compatible")
	}
}
func makeEndpoints(body string) []mockingjay.FakeEndpoint {
	e, _ := mockingjay.NewFakeEndpoints([]byte(testYAML(body)))
	return e
}