Skip to content

Commit 9a62581

Browse files
feat: add endpoint for testing x-ndjson (#27)
1 parent 13f441d commit 9a62581

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

cmd/server/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/speakeasy-api/speakeasy-api-test-service/internal/reflect"
2121
"github.com/speakeasy-api/speakeasy-api-test-service/internal/responseHeaders"
2222
"github.com/speakeasy-api/speakeasy-api-test-service/internal/retries"
23+
"github.com/speakeasy-api/speakeasy-api-test-service/internal/xNdJson"
2324

2425
"github.com/gorilla/mux"
2526
"github.com/speakeasy-api/speakeasy-api-test-service/internal/auth"
@@ -66,6 +67,8 @@ func main() {
6667
r.HandleFunc("/eventstreams/differentdataschemas-flat", eventstreams.HandleEventStreamDifferentDataSchemasFlatten).Methods(http.MethodPost)
6768
r.HandleFunc("/jsonl", jsonLines.HandleJSONLinesRich).Methods(http.MethodGet)
6869
r.HandleFunc("/jsonl/chunks", jsonLines.HandleJSONLinesChunksRich).Methods(http.MethodGet)
70+
r.HandleFunc("/x-ndjson", xNdJson.HandleXNdJsonLinesRich).Methods(http.MethodGet)
71+
r.HandleFunc("/x-ndjson/chunks", xNdJson.HandleXNdJsonLinesChunksRich).Methods(http.MethodGet)
6972
r.HandleFunc("/clientcredentials/token", clientcredentials.HandleTokenRequest).Methods(http.MethodPost)
7073
r.HandleFunc("/clientcredentials/authenticatedrequest", clientcredentials.HandleAuthenticatedRequest).Methods(http.MethodPost)
7174
r.HandleFunc("/clientcredentials/alt/token", clientcredentials.HandleTokenRequest).Methods(http.MethodPost)

internal/xNdJson/service.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package xNdJson
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"time"
7+
)
8+
9+
func pushEvents(rw http.ResponseWriter, events [][]string) {
10+
for _, event := range events {
11+
for _, line := range event {
12+
fmt.Fprint(rw, line)
13+
}
14+
15+
if f, ok := rw.(http.Flusher); ok {
16+
f.Flush()
17+
}
18+
19+
time.Sleep(100 * time.Millisecond)
20+
}
21+
}
22+
23+
func pushChunks(rw http.ResponseWriter, chunks []string) {
24+
for _, chunk := range chunks {
25+
fmt.Fprint(rw, chunk)
26+
27+
if f, ok := rw.(http.Flusher); ok {
28+
f.Flush()
29+
}
30+
31+
time.Sleep(100 * time.Millisecond)
32+
}
33+
}
34+
35+
func HandleXNdJsonLinesChunksRich(rw http.ResponseWriter, _ *http.Request) {
36+
rw.Header().Add("Content-Type", "application/x-ndjson")
37+
38+
pushChunks(rw, []string{
39+
"{\"name\": \"Peter\", \"skills\": [\"Go\"",
40+
", \"Python\"]}\n{\"name\": \"John\"",
41+
", \"skills\": [\"Go\", \"Rust\"]}\n",
42+
})
43+
}
44+
45+
func HandleXNdJsonLinesRich(rw http.ResponseWriter, _ *http.Request) {
46+
rw.Header().Add("Content-Type", "application/x-ndjson")
47+
48+
pushEvents(rw, [][]string{
49+
{
50+
"{\"name\": \"Peter\", \"skills\": [\"Go\", \"Python\"]}\n",
51+
},
52+
{
53+
"{\"name\": \"John\", \"skills\": [\"Go\", \"Rust\"]}\n",
54+
},
55+
})
56+
}

0 commit comments

Comments
 (0)