1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/integration-cli/docker_api_events_test.go
Jessica Frazelle d2f75a3040 Fix line delimited JSON response
For GET /events, line delimit JSON.
Fixes #7047

Signed-off-by: Jessica Frazelle <jfrazelle@users.noreply.github.com>

Docker-DCO-1.1-Signed-off-by: Jessica Frazelle <jfrazelle@users.noreply.github.com> (github: )
2014-09-08 09:41:26 -07:00

57 lines
1.1 KiB
Go

package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"testing"
"time"
)
func TestGetEventsLineDelim(t *testing.T) {
name := "testimageevents"
defer deleteImages(name)
_, err := buildImage(name,
`FROM scratch
MAINTAINER "docker"`,
true)
if err != nil {
t.Fatal(err)
}
if err := deleteImages(name); err != nil {
t.Fatal(err)
}
endpoint := fmt.Sprintf("/events?since=1&until=%d", time.Now().Unix())
body, err := sockRequest("GET", endpoint)
if err != nil {
t.Fatal(err)
}
linesRead := 0
scanner := bufio.NewScanner(bytes.NewReader(body))
for scanner.Scan() && linesRead < 2 {
line := scanner.Bytes()
if len(line) == 0 {
continue
}
// make sure line delimited json
res := make(map[string]interface{})
if err := json.Unmarshal(line, &res); err != nil {
t.Fatalf("Unmarshaling the line as JSON failed: %v", err)
}
linesRead++
}
if err := scanner.Err(); err != nil {
t.Fatalf("Scanner failed: %v", err)
}
if linesRead < 2 {
t.Fatalf("Only %d lines were read from the stream", linesRead)
}
logDone("events - test the api response is line delimited json")
}