/usr/share/gocode/src/collectd.org/api/json_test.go is in golang-collectd-dev 0.0~git20150630-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | package api
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"reflect"
"testing"
"time"
)
func TestValueList(t *testing.T) {
vlWant := ValueList{
Identifier: Identifier{
Host: "example.com",
Plugin: "golang",
Type: "gauge",
},
Time: time.Unix(1426585562, 999000000),
Interval: 10 * time.Second,
Values: []Value{Gauge(42)},
DSNames: []string{"legacy"},
}
want := `{"values":[42],"dstypes":["gauge"],"dsnames":["legacy"],"time":1426585562.999,"interval":10.000,"host":"example.com","plugin":"golang","type":"gauge"}`
got, err := vlWant.MarshalJSON()
if err != nil || string(got) != want {
t.Errorf("got (%s, %v), want (%s, nil)", got, err, want)
}
var vlGot ValueList
if err := vlGot.UnmarshalJSON([]byte(want)); err != nil {
t.Errorf("got %v, want nil)", err)
}
// Conversion to float64 and back takes its toll -- the conversion is
// very accurate, but not bit-perfect.
vlGot.Time = vlGot.Time.Round(time.Millisecond)
if !reflect.DeepEqual(vlWant, vlGot) {
t.Errorf("got %#v, want %#v)", vlGot, vlWant)
}
}
func ExampleValueList_UnmarshalJSON() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("while reading body: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var vls []ValueList
if err := json.Unmarshal(data, &vls); err != nil {
log.Printf("while parsing JSON: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
for _, vl := range vls {
var w Writer
w.Write(vl)
// "w" is a placeholder to avoid cyclic dependencies.
// In real live, you'd do something like this here:
// exec.Putval.Write(vl)
}
w.WriteHeader(http.StatusNoContent)
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
|