telegraf/plugins/inputs/webhooks/particle/particle_webhooks_test.go

58 lines
1.4 KiB
Go
Raw Normal View History

2017-10-02 20:50:23 +00:00
package particle
import (
"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))
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())
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,
"broadband": 1266,
"infrared": 528,
"lux": 0,
2017-10-02 20:50:23 +00:00
}
tags := map[string]string{
"id": "230035001147343438323536",
"location": "TravelingWilbury",
}
acc.AssertContainsTaggedFields(t, "particle_webhooks", fields, tags)
2017-10-02 20:50:23 +00:00
}
func TestUnknowItem(t *testing.T) {
rb := &ParticleWebhook{Path: "/particle"}
2017-10-02 20:50:23 +00:00
resp := postWebhooks(rb, UnknowJSON())
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)
}
}