|
1 |
| -package sseread |
| 1 | +package sseread_test |
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "io" |
5 |
| - "log" |
| 4 | + "fmt" |
| 5 | + "github.com/mojocn/sseread" |
6 | 6 | "net/http"
|
7 | 7 | "strings"
|
8 |
| - "testing" |
9 | 8 | )
|
10 | 9 |
|
11 |
| -// TestRead is a test function for the Read function in the sseread package. |
12 |
| -// It sends a GET request to the specified URL and reads the response body as Server-Sent Events. |
13 |
| -// For each event, it appends it to the messages slice and logs the event ID, event type, and event data. |
14 |
| -// If an error occurs during the GET request or the reading of the events, it fails the test. |
15 |
| -func TestRead(t *testing.T) { |
| 10 | +// ExampleRead is a function that demonstrates how to read Server-Sent Events (SSE) from a specific URL. |
| 11 | +func ExampleRead() { |
| 12 | + // Send a GET request to the specified URL. |
16 | 13 | response, err := http.Get("https://mojotv.cn/api/sse") //API source code from https://github.com/mojocn/gptchat/blob/main/app/api/sse/route.ts
|
| 14 | + // If an error occurs during the GET request, print the error and return from the function. |
17 | 15 | if err != nil {
|
18 |
| - t.Fatal(err) |
| 16 | + fmt.Println(err) |
| 17 | + return |
19 | 18 | }
|
20 | 19 |
|
| 20 | + // Get the Content-Type header from the response and convert it to lowercase. |
21 | 21 | ct := strings.ToLower(response.Header.Get("Content-Type"))
|
| 22 | + // If the Content-Type is not "text/event-stream", print a message and continue. |
22 | 23 | if !strings.Contains(ct, "text/event-stream") {
|
23 |
| - t.Fatal("expect content-type: text/event-stream, but actual", ct) |
| 24 | + fmt.Println("expect content-type: text/event-stream, but actual", ct) |
24 | 25 | }
|
25 | 26 |
|
26 |
| - defer safeClose(response.Body) //don't forget to close the response body |
| 27 | + // Ensure the response body is closed when the function returns. |
| 28 | + defer response.Body.Close() //don't forget to close the response body |
27 | 29 |
|
28 |
| - var messages []Event |
29 |
| - err = Read(response.Body, func(msg *Event) { |
| 30 | + // Declare a slice to store the SSE messages. |
| 31 | + var messages []sseread.Event |
| 32 | + // Read the SSE messages from the response body. |
| 33 | + err = sseread.Read(response.Body, func(msg *sseread.Event) { |
| 34 | + // If the message is not nil, append it to the messages slice. |
30 | 35 | if msg != nil {
|
31 | 36 | messages = append(messages, *msg)
|
32 | 37 | }
|
33 |
| - t.Log(msg.ID, msg.Event, string(msg.Data)) |
| 38 | + // Print the ID, event type, and data of the message. |
| 39 | + fmt.Println(msg.ID, msg.Event, string(msg.Data)) |
34 | 40 | })
|
| 41 | + // If an error occurs while reading the SSE messages, print the error and return from the function. |
35 | 42 | if err != nil {
|
36 |
| - t.Fatal(err) |
| 43 | + fmt.Println(err) |
| 44 | + return |
37 | 45 | }
|
38 |
| - t.Logf("length of messages is %d", len(messages)) |
| 46 | + // Print the number of messages read. |
| 47 | + fmt.Printf("length of messages is %d", len(messages)) |
39 | 48 | }
|
40 | 49 |
|
41 |
| -// TestReadChannel is a test function for the ReadCh function in the sseread package. |
42 |
| -// It sends a GET request to the specified URL and reads the response body as Server-Sent Events. |
43 |
| -// For each event, it logs the event ID, event type, and event data. |
44 |
| -// If an error occurs during the GET request or the reading of the events, it fails the test. |
45 |
| -func TestReadChannel(t *testing.T) { |
| 50 | +// ExampleReadCh is a function that demonstrates how to read Server-Sent Events (SSE) from a specific URL using channels. |
| 51 | +func ExampleReadCh() { |
| 52 | + // Send a GET request to the specified URL. |
46 | 53 | response, err := http.Get("https://mojotv.cn/api/sse") //API source code from https://github.com/mojocn/gptchat/blob/main/app/api/sse/route.ts
|
| 54 | + // If an error occurs during the GET request, print the error and return from the function. |
47 | 55 | if err != nil {
|
48 |
| - t.Fatal(err) |
| 56 | + fmt.Println(err) |
| 57 | + return |
49 | 58 | }
|
50 |
| - defer safeClose(response.Body) //don't forget to close the response body |
51 | 59 |
|
| 60 | + // Get the Content-Type header from the response and convert it to lowercase. |
52 | 61 | ct := strings.ToLower(response.Header.Get("Content-Type"))
|
| 62 | + // If the Content-Type is not "text/event-stream", print a message and continue. |
53 | 63 | if !strings.Contains(ct, "text/event-stream") {
|
54 |
| - t.Fatal("expect content-type: text/event-stream, but actual", ct) |
| 64 | + fmt.Println("expect content-type: text/event-stream, but actual", ct) |
55 | 65 | }
|
56 | 66 |
|
57 |
| - channel, err := ReadCh(response.Body) |
| 67 | + // Ensure the response body is closed when the function returns. |
| 68 | + defer response.Body.Close() //don't forget to close the response body |
| 69 | + |
| 70 | + // Read the SSE messages from the response body into a channel. |
| 71 | + channel, err := sseread.ReadCh(response.Body) |
| 72 | + // If an error occurs while reading the SSE messages, print the error and return from the function. |
58 | 73 | if err != nil {
|
59 |
| - t.Fatal(err) |
| 74 | + fmt.Println(err) |
| 75 | + return |
60 | 76 | }
|
61 |
| - var messages []Event |
| 77 | + // Declare a slice to store the SSE messages. |
| 78 | + var messages []sseread.Event |
| 79 | + // Loop over the channel to receive the SSE messages. |
62 | 80 | for msg := range channel {
|
| 81 | + // If the message is not nil, append it to the messages slice. |
63 | 82 | if msg != nil {
|
64 | 83 | messages = append(messages, *msg)
|
65 | 84 | }
|
66 |
| - t.Log(msg.ID, msg.Event, string(msg.Data)) |
67 |
| - } |
68 |
| - t.Logf("length of messages is %d", len(messages)) |
69 |
| - |
70 |
| -} |
71 |
| - |
72 |
| -func safeClose(closer io.Closer) { |
73 |
| - if err := closer.Close(); err != nil { |
74 |
| - log.Println("error closing:", err) |
| 85 | + // Print the ID, event type, and data of the message. |
| 86 | + fmt.Println(msg.ID, msg.Event, string(msg.Data)) |
75 | 87 | }
|
| 88 | + // Print the number of messages read. |
| 89 | + fmt.Printf("length of messages is %d", len(messages)) |
76 | 90 | }
|
0 commit comments