2017-10-02 20:50:23 +00:00
|
|
|
package particle
|
|
|
|
|
|
|
|
import (
|
2017-11-03 17:28:35 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
"log"
|
2017-10-02 20:50:23 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func postWebhooks(rb *ParticleWebhook, eventBody string) *httptest.ResponseRecorder {
|
|
|
|
req, _ := http.NewRequest("POST", "/", strings.NewReader(eventBody))
|
2017-11-03 17:28:35 +00:00
|
|
|
log.Printf("eventBody: %s\n", eventBody)
|
2017-10-02 20:50:23 +00:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
w.Code = 500
|
|
|
|
|
|
|
|
rb.eventHandler(w, req)
|
|
|
|
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewItem(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
rb := &ParticleWebhook{Path: "/particle", acc: &acc}
|
|
|
|
resp := postWebhooks(rb, NewItemJSON())
|
2017-11-03 17:28:35 +00:00
|
|
|
log.Printf("Respnse: %s\n", resp.Body)
|
2017-10-02 20:50:23 +00:00
|
|
|
if resp.Code != http.StatusOK {
|
|
|
|
t.Errorf("POST new_item returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := map[string]interface{}{
|
|
|
|
"temp_c": 26.680000,
|
|
|
|
"temp_f": 80.024001,
|
|
|
|
"humidity": 44.937500,
|
|
|
|
"pressure": 998.998901,
|
|
|
|
"altitude": 119.331436,
|
2017-11-03 17:28:35 +00:00
|
|
|
"broadband": 1266,
|
|
|
|
"infrared": 528,
|
|
|
|
"lux": 0,
|
2017-10-02 20:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tags := map[string]string{
|
|
|
|
"id": "230035001147343438323536",
|
|
|
|
"location": "TravelingWilbury",
|
|
|
|
}
|
|
|
|
|
2017-11-03 17:28:35 +00:00
|
|
|
acc.AssertContainsTaggedFields(t, "particle_webhooks", fields, tags)
|
2017-10-02 20:50:23 +00:00
|
|
|
}
|
|
|
|
func TestUnknowItem(t *testing.T) {
|
2017-11-03 17:28:35 +00:00
|
|
|
rb := &ParticleWebhook{Path: "/particle"}
|
2017-10-02 20:50:23 +00:00
|
|
|
resp := postWebhooks(rb, UnknowJSON())
|
2017-11-03 17:28:35 +00:00
|
|
|
log.Printf("Response: %s\n", resp.Body)
|
2017-10-02 20:50:23 +00:00
|
|
|
if resp.Code != http.StatusOK {
|
|
|
|
t.Errorf("POST unknown returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
|
|
|
|
}
|
|
|
|
}
|