github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/test/count/server/server_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "net/http" 8 "regexp" 9 "testing" 10 "time" 11 12 client "github.com/lmorg/murex/test/count" 13 ) 14 15 // There are two man purposes to this test: 16 // 17 // 1. Test the code actually compiles (since it's a separate project within the 18 // murex project hierarchy) 19 // 2. Test the machine readable total API doesn't suffer a regression bug 20 func TestServer(t *testing.T) { 21 client.Tests(t, 1) 22 23 port-- 24 25 var err error 26 go func() { 27 err = http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), testHTTPHandler{}) 28 }() 29 30 time.Sleep(500 * time.Millisecond) 31 32 if err != nil { 33 // lets not get caught up with testing if there is already a listener 34 t.SkipNow() 35 } 36 37 client.Tests(t, 2) 38 39 testCount(t) 40 //testT(t) //this test doesn't yet work 41 //os.Exit(0) 42 } 43 44 func testCount(t *testing.T) { 45 buf := new(bytes.Buffer) 46 _, err := buf.WriteString("1") 47 if err != nil { 48 t.Errorf("Unable to log test count via HTTP (export %s=http): %s", client.Env, err) 49 return 50 } 51 52 req, err := http.Post(fmt.Sprintf("http://%s:%d/count", host, port), "int", buf) 53 if err != nil { 54 t.Errorf("Unable to log test count via HTTP (export %s=http): %s", client.Env, err) 55 return 56 } 57 58 b, err := io.ReadAll(req.Body) 59 if err != nil { 60 t.Errorf("Potential mismatch logging test counts via HTTP (export %s=http): %s", client.Env, err) 61 } 62 63 if string(b) != "OK" { 64 t.Errorf("Potential mismatch logging test counts via HTTP (export %s=http): %s", client.Env, `Body != "OK"`) 65 } 66 } 67 68 func testT(t *testing.T) { 69 req, err := http.Get(fmt.Sprintf("http://%s:%d/t", host, port)) 70 if err != nil { 71 t.Errorf("Unable to log test count via HTTP (export %s=http): %s", client.Env, err) 72 return 73 } 74 75 b, err := io.ReadAll(req.Body) 76 if err != nil { 77 t.Errorf("Potential mismatch logging test counts via HTTP (export %s=http): %s", client.Env, err) 78 } 79 80 regex := `^[0-9]+$` 81 rx := regexp.MustCompile(regex) 82 if !rx.Match(b) { 83 t.Errorf("Potential mismatch logging test counts via HTTP (export %s=http): Body !~ %s", client.Env, regex) 84 } 85 }