示例#1
0
func TestServerDrop(t *testing.T) {
	// drop a request
	// launch the server
	doSimpleServer()

	seenTwo := false
	time.Sleep(1 * time.Second)
	expectedUrls := []string{"one", "two", "three", "two"}
	for _, expected := range expectedUrls {

		request, err := client.Get("http://localhost:8080")
		PanicErr(err)
		if request.URL != expected {
			t.Errorf("expected:", expected, "recieved:", request.URL, "\n")
		}

		fmt.Println("got request:", request.URL)
		if request.URL != "" {
			response := netScraper.Response{URL: request.URL, Data: "data", Error: netScraper.ResponseOk}
			if request.URL == "two" && !seenTwo {
				seenTwo = true
				// don't send
			} else {
				_ = client.Post("http://localhost:8080", response)
			}
		}

		time.Sleep(time.Duration(4) * time.Second)
	}
}
示例#2
0
func TestServerSimple(t *testing.T) {
	// acts like a client
	// starts the server then adds 3 articles
	// the client then reads the articles faster than
	// they come out, so it looks like there is no work there

	// launch the server
	doSimpleServer()
	time.Sleep(1 * time.Second)

	expectedUrls := []string{"one", "two", "three"}
	for _, expected := range expectedUrls {

		fmt.Println("client fetching")
		request, err := client.Get("http://localhost:8080")
		PanicErr(err)
		if request.URL != expected {
			t.Errorf("expected:", expected, "recieved:", request.URL, "\n")
		}

		empty, err := client.Get("http://localhost:8080")
		PanicErr(err)
		if !netScraper.IsEmptyRequest(empty) {
			t.Errorf("expected empty request, got:", empty.URL)
		}

		if request.URL != "" {
			response := netScraper.Response{URL: request.URL, Data: "data", Error: netScraper.ResponseOk}
			client.Post("http://localhost:8080", response)
		}

		time.Sleep(time.Duration(4) * time.Second)
	}
}
示例#3
0
func TestServerMultiLatePostRerun(t *testing.T) {
	// respond late to a request
	doSimpleServer()

	seenTwo := false
	expectedUrls := []string{"one", "two", "three", "two"}
	time.Sleep(1 * time.Second)
	i := 0
	for _, expected := range expectedUrls {

		request, err := client.Get("http://localhost:8080")
		PanicErr(err)
		if request.URL != expected {
			t.Errorf("expected: %s recieved: %s\n", request.URL, expected)
		}
		i++
		fmt.Println("got request:", request.URL)
		if request.URL != "" {
			response := netScraper.Response{URL: request.URL, Data: "data", Error: netScraper.ResponseOk}
			if request.URL == "two" && !seenTwo {
				seenTwo = true
				go func() {
					time.Sleep(time.Duration(12) * time.Second)
					client.Post("http://localhost:8080", response)
				}()
			} else {
				client.Post("http://localhost:8080", response)
			}

		}

		time.Sleep(time.Duration(4) * time.Second)
	}

	if i != len(expectedUrls) {
		t.Errorf("did not get through all the urls")
	}
}