forked from speakeasy-api/speakeasy-auth-test-service
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add event streaming test endpoints (#5)
This change adds mock endpoints that stream server-sent events. These will support the test suite in openapi-generation. Also added the ability to run the server using a different bind address. This is helpful when running the project locally outside of docker.
- Loading branch information
1 parent
6387eaf
commit 3dc7b02
Showing
3 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package eventstreams | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func pushEvents(rw http.ResponseWriter, events [][]string) { | ||
for _, event := range events { | ||
for _, line := range event { | ||
fmt.Fprintln(rw, line) | ||
} | ||
fmt.Fprintln(rw, "") | ||
|
||
if f, ok := rw.(http.Flusher); ok { | ||
f.Flush() | ||
} | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
} | ||
} | ||
|
||
func HandleEventStreamJSON(rw http.ResponseWriter, _ *http.Request) { | ||
rw.Header().Add("Content-Type", "text/event-stream") | ||
|
||
pushEvents(rw, [][]string{ | ||
{ | ||
`data: {"content": "Hello"}`, | ||
}, | ||
|
||
{ | ||
`data: {"content": " "}`, | ||
}, | ||
|
||
{ | ||
`data: {"content": "world"}`, | ||
}, | ||
|
||
{ | ||
`data: {"content": "!"}`, | ||
}, | ||
}) | ||
} | ||
|
||
func HandleEventStreamText(rw http.ResponseWriter, _ *http.Request) { | ||
rw.Header().Add("Content-Type", "text/event-stream") | ||
|
||
pushEvents(rw, [][]string{ | ||
{ | ||
`data: Hello`, | ||
}, | ||
|
||
{ | ||
`data: `, | ||
}, | ||
|
||
{ | ||
`data: world`, | ||
}, | ||
|
||
{ | ||
`data: !`, | ||
}, | ||
}) | ||
} | ||
|
||
func HandleEventStreamMultiLine(rw http.ResponseWriter, _ *http.Request) { | ||
rw.Header().Add("Content-Type", "text/event-stream") | ||
|
||
pushEvents(rw, [][]string{ | ||
{ | ||
`data: YHOO`, | ||
`data: +2`, | ||
`data: 10`, | ||
}, | ||
}) | ||
} | ||
|
||
func HandleEventStreamRich(rw http.ResponseWriter, _ *http.Request) { | ||
rw.Header().Add("Content-Type", "text/event-stream") | ||
|
||
pushEvents(rw, [][]string{ | ||
{ | ||
`id: job-1`, | ||
`event: completion`, | ||
`data: {"completion": "Hello", "stop_reason": null, "model": "jeeves-1"}`, | ||
}, | ||
|
||
{ | ||
`event: heartbeat`, | ||
`data: ping`, | ||
`retry: 3000`, | ||
}, | ||
|
||
{ | ||
`id: job-1`, | ||
`event: completion`, | ||
`data: {"completion": "world!", "stop_reason": "stop_sequence", "model": "jeeves-1"}`, | ||
}, | ||
}) | ||
} | ||
|
||
func HandleEventStreamChat(rw http.ResponseWriter, _ *http.Request) { | ||
rw.Header().Add("Content-Type", "text/event-stream") | ||
|
||
pushEvents(rw, [][]string{ | ||
{ | ||
`data: {"content": "Hello"}`, | ||
}, | ||
|
||
{ | ||
`data: {"content": " "}`, | ||
}, | ||
|
||
{ | ||
`data: {"content": "world"}`, | ||
}, | ||
|
||
{ | ||
`data: {"content": "!"}`, | ||
}, | ||
|
||
{ | ||
`data: [DONE]`, | ||
}, | ||
}) | ||
} |