From 64e9121e5b263d6c5b402157c015b88b0212886e Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 8 Oct 2014 17:00:41 +0100 Subject: [PATCH] Simple test of http function --- server/http_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 server/http_test.go diff --git a/server/http_test.go b/server/http_test.go new file mode 100644 index 00000000..53b7cbf3 --- /dev/null +++ b/server/http_test.go @@ -0,0 +1,47 @@ +package weavedns + +import ( + "net" + "net/http" + "net/url" + "testing" + "time" +) + +func TestHttp(t *testing.T) { + var ( + success_test_name = "test1.weave." + test_addr1 = "10.0.2.1/24" + docker_ip = "9.8.7.6" + ) + + var zone = new(ZoneDb) + go ListenHttp(zone) + + time.Sleep(100 * time.Millisecond) // Allow for http server to get going + + resp, err := http.PostForm("http://localhost:6785/add", + url.Values{"name": {success_test_name}, "ip": {docker_ip}, "weave_cidr": {test_addr1}}) + if err != nil { + t.Fatal(err) + } + if resp.StatusCode != 200 { + t.Log("Unexpected http response", resp.Status) + t.Fail() + } + + ip, err := zone.MatchLocal(success_test_name) + if err != nil { + t.Log(zone) + t.Fatal(err) + } + weave_ip, _, _ := net.ParseCIDR(test_addr1) + if !ip.Equal(weave_ip) { + t.Log("Unexpected result for", success_test_name, ip) + t.Fail() + } + + // Would like to shut down the http server at the end of this test + // but it's complicated. + // See https://groups.google.com/forum/#!topic/golang-nuts/vLHWa5sHnCE +}