From 781a614b3166d3bd6ab8fe63e1c72e58293acb3c Mon Sep 17 00:00:00 2001 From: Jakub Jarosz Date: Fri, 8 Mar 2024 20:06:26 +0000 Subject: [PATCH] Add initial test for web client --- hpot_test.go | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/hpot_test.go b/hpot_test.go index fe4cdca..b305cf1 100644 --- a/hpot_test.go +++ b/hpot_test.go @@ -2,7 +2,10 @@ package hpot_test import ( "fmt" + "io" "net" + "net/http" + "net/http/httptest" "testing" "time" @@ -23,18 +26,19 @@ func TestHoneypotAcceptsConection(t *testing.T) { client1 := mustConnect(t, port1) client2 := mustConnect(t, port2) - client3 := mustConnect(t, port1) client4 := mustConnect(t, port2) want := []net.Addr{client1, client2, client3, client4} - // Fix race conditions here - check ports! - // temp hack!!! - time.Sleep(2 * time.Second) - got := pot.Records() + // wait for success + for len(got) < 4 { + time.Sleep(10 * time.Millisecond) + got = pot.Records() + } + if !cmp.Equal(want, got) { t.Error(cmp.Diff(want, got)) } @@ -63,3 +67,35 @@ func mustConnect(t *testing.T, port int) net.Addr { defer conn.Close() return conn.LocalAddr() } + +func TestPotStervesHTTPStatusPage(t *testing.T) { + t.Parallel() + + ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, t *http.Request) { + fmt.Fprint(w, "Honeypot stats") + })) + defer ts.Close() + + client := ts.Client() + + // user behaviour + res, err := client.Get(ts.URL) + if err != nil { + t.Fatal(err) + } + defer res.Body.Close() + + if res.StatusCode != http.StatusOK { + t.Fatal(res.StatusCode) + } + + got, err := io.ReadAll(res.Body) + if err != nil { + t.Fatal(err) + } + + want := "Honeypot stats" + if want != string(got) { + t.Errorf("want %s, got %s", want, got) + } +}