From 353881876993184521435818b9b811f9c2ec53a2 Mon Sep 17 00:00:00 2001 From: vinchent Date: Wed, 18 Sep 2024 12:45:58 +0200 Subject: [PATCH] racer: test with httptest server --- racer/racer_test.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/racer/racer_test.go b/racer/racer_test.go index 691bfb0..07f29c7 100644 --- a/racer/racer_test.go +++ b/racer/racer_test.go @@ -1,10 +1,28 @@ package racer -import "testing" +import ( + "net/http" + "net/http/httptest" + "testing" + "time" +) func TestRacer(t *testing.T) { - slowURL := "http://www.facebook.com" - fastURL := "http://www.quii.dev" + slowServer := httptest.NewServer(http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + time.Sleep(20 * time.Millisecond) + w.WriteHeader(http.StatusOK) + })) + defer slowServer.Close() + + fastServer := httptest.NewServer(http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + })) + defer fastServer.Close() + + slowURL := slowServer.URL + fastURL := fastServer.URL want := fastURL got := Racer(slowURL, fastURL)