Skip to content

Commit

Permalink
Add initial test for web client
Browse files Browse the repository at this point in the history
  • Loading branch information
qba73 committed Mar 8, 2024
1 parent 41ec7c3 commit 781a614
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions hpot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package hpot_test

import (
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"

Expand All @@ -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))
}
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 781a614

Please # to comment.