mirror of
https://github.com/clearlinux/docker.git
synced 2026-10-04 15:58:39 +00:00
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: )
This commit is contained in:
committed by
Jessica Frazelle
parent
68e07f3fa0
commit
d2f75a3040
@@ -0,0 +1,57 @@
|
||||
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")
|
||||
}
|
||||
Reference in New Issue
Block a user