github.com/orofarne/hammy@v0.0.0-20130409105742-374fadfd6ecb/src/hammy/http_test.go (about) 1 package hammy 2 3 import ( 4 "testing" 5 "bytes" 6 "net/http" 7 "time" 8 //"fmt" 9 "io/ioutil" 10 "encoding/json" 11 "github.com/ugorji/go-msgpack" 12 ) 13 14 type RequestHandlerTestImpl struct { 15 Data IncomingData 16 } 17 18 func (rh *RequestHandlerTestImpl) Handle(data IncomingData) (errs map[string]error) { 19 errs = make(map[string]error) 20 rh.Data = data 21 //fmt.Printf("--> Got new data:\n%#v\n\n", data) 22 return 23 } 24 25 func TestHttpInterface(t *testing.T) { 26 var cfg Config 27 cfg.IncomingHttp.Addr = "127.0.0.1:16739" 28 SetConfigDefaults(&cfg) 29 rh := new(RequestHandlerTestImpl) 30 31 go StartHttp(rh, cfg, "testHttpServerInstance") 32 time.Sleep(100 * time.Millisecond) 33 34 httpAddr := "http://" + cfg.IncomingHttp.Addr + "/" 35 36 // Simple test (Empty but valid data) 37 buf := bytes.NewBufferString("{}") 38 resp, err := http.Post(httpAddr, "application/json", buf) 39 if err != nil { t.Fatalf("Got error: %v", err) } 40 if resp.StatusCode != 200 { 41 t.Errorf("Expected response of code 200, got: %#v", resp) 42 } 43 44 // Simple errors 45 resp, err = http.Get(httpAddr) 46 if err != nil { t.Fatalf("Got error: %v", err) } 47 if resp.StatusCode != 405 { 48 t.Errorf("Expected 405, got: %#v", resp) 49 } 50 51 buf = bytes.NewBufferString("asdfasdfadsfa") 52 resp, err = http.Post(httpAddr, "application/json", buf) 53 if err != nil { t.Fatalf("Got error: %v", err) } 54 if resp.StatusCode != 400 { 55 t.Errorf("Expected response of code 400, got: %#v", resp) 56 } 57 58 // Data and checker 59 jsonTestData := `{ 60 "data": { 61 "host1": { 62 "key1.1": [{ 63 "timestamp": 1361785778, 64 "value": 3.14 65 }] 66 }, 67 "host2": { 68 "key2.1": [{ 69 "timestamp": 1361785817, 70 "value": "test string" 71 }], 72 "key2.2": [{ 73 "timestamp": 1361785858, 74 "value": 12345 75 }, 76 { 77 "timestamp": 1361785927, 78 "value": 999.3 79 }] 80 } 81 } 82 }` 83 84 checkTestData := func() { 85 if host1, found := rh.Data["host1"]; !found { 86 t.Errorf("`host1` not found") 87 } else { 88 if key11, found := host1["key1.1"]; !found { 89 t.Errorf("`key1.1` not found") 90 } else { 91 if len(key11) != 1 { 92 t.Errorf("Expected len(key11) = 1, got: %d", len(key11)) 93 } else { 94 if key11[0].Timestamp != 1361785778 { 95 t.Errorf("Expected %v, got %#v", 1361785778, key11[0].Timestamp) 96 } 97 val, converted := key11[0].Value.(float64) 98 if !converted { t.Errorf("Wrong type %T", key11[0].Value) } else { 99 if val != 3.14 { 100 t.Errorf("Expected %v, got %#v", 3.14, val) 101 } 102 } 103 } 104 } 105 } 106 if host2, found := rh.Data["host2"]; !found { 107 t.Errorf("`host2` not found") 108 } else { 109 if key21, found := host2["key2.1"]; !found { 110 t.Errorf("`key2.1` not found") 111 } else { 112 if len(key21) != 1 { 113 t.Errorf("Expected len(key21) = 1, got: %d", len(key21)) 114 } else { 115 if key21[0].Timestamp != 1361785817 { 116 t.Errorf("Expected %v, got %#v", 1361785817, key21[0].Timestamp) 117 } 118 val, converted := key21[0].Value.(string) 119 if !converted { t.Errorf("Wrong type %T", key21[0].Value) } else { 120 if val != "test string" { 121 t.Errorf("Expected %v, got %#v", "test string", val) 122 } 123 } 124 } 125 } 126 if key22, found := host2["key2.2"]; !found { 127 t.Errorf("`key2.2` not found") 128 } else { 129 if len(key22) != 2 { 130 t.Errorf("Expected len(key22) = 2, got: %d", len(key22)) 131 } else { 132 if key22[0].Timestamp != 1361785858 { 133 t.Errorf("Expected %v, got %#v", 1361785858, key22[0].Timestamp) 134 } 135 val, converted := key22[0].Value.(float64) 136 if !converted { t.Errorf("Wrong type %T", key22[0].Value) } else { 137 if val != 12345 { 138 t.Errorf("Expected %v, got %#v", 12345, val) 139 } 140 } 141 if key22[1].Timestamp != 1361785927 { 142 t.Errorf("Expected %v, got %#v", 1361785927, key22[1].Timestamp) 143 } 144 val, converted = key22[1].Value.(float64) 145 if !converted { t.Errorf("Wrong type %T", key22[1].Value) } else { 146 if val != 999.3 { 147 t.Errorf("Expected %v, got %#v", 999.3, val) 148 } 149 } 150 } 151 } 152 } 153 } 154 155 // JSON 156 // Valid 157 buf = bytes.NewBufferString(jsonTestData) 158 resp, err = http.Post(httpAddr, "application/json", buf) 159 if err != nil { t.Fatalf("Got error: %v", err) } 160 if resp.StatusCode != 200 { 161 body, err := ioutil.ReadAll(resp.Body) 162 if err != nil { panic("Failed to read body") } 163 t.Errorf("Expected response of code 200, got: %#v, body: %s", resp, body) 164 } 165 checkTestData() 166 167 // Invalid 168 buf = bytes.NewBufferString(`{ 169 "data": { 170 "host1": { 171 "key1": "booo!" 172 } 173 } 174 }`) 175 resp, err = http.Post(httpAddr, "application/json", buf) 176 if err != nil { t.Fatalf("Got error: %v", err) } 177 if resp.StatusCode != 400 { 178 body, err := ioutil.ReadAll(resp.Body) 179 if err != nil { panic("failed to read body") } 180 t.Errorf("Expected response of code 400, got: %#v, body: %s", resp, body) 181 } 182 183 // Message Pack 184 // Valid 185 var buf_msg IncomingMessage 186 err = json.Unmarshal([]byte(jsonTestData), &buf_msg) 187 if err != nil { panic("error unmarshaling test data") } 188 enc := msgpack.NewEncoder(buf) 189 err = enc.Encode(buf_msg) 190 if err != nil { panic("error remarshaling (to msgpack) test data") } 191 192 resp, err = http.Post(httpAddr, "application/x-msgpack", buf) 193 if err != nil { t.Fatalf("Got error: %v", err) } 194 if resp.StatusCode != 200 { 195 body, err := ioutil.ReadAll(resp.Body) 196 if err != nil { panic("Failed to read body") } 197 t.Errorf("Expected response of code 200, got: %#v, body: %s", resp, body) 198 } 199 checkTestData() 200 201 // Invalid 202 buf = bytes.NewBufferString("adsfasdfasdfasdfasdfasdfasdfasdfasdfasdfasd") 203 resp, err = http.Post(httpAddr, "application/x-msgpack", buf) 204 if err != nil { t.Fatalf("Got error: %v", err) } 205 if resp.StatusCode != 400 { 206 body, err := ioutil.ReadAll(resp.Body) 207 if err != nil { panic("failed to read body") } 208 t.Errorf("Expected response of code 400, got: %#v, body: %s", resp, body) 209 } 210 }