Skip to content

Commit 1272950

Browse files
author
CodeApe
committed
add example_test.go
1 parent 356ee88 commit 1272950

File tree

3 files changed

+129
-119
lines changed

3 files changed

+129
-119
lines changed

example_test.go

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,90 @@
1-
package sseread
1+
package sseread_test
22

33
import (
4-
"io"
5-
"log"
4+
"fmt"
5+
"github.com/mojocn/sseread"
66
"net/http"
77
"strings"
8-
"testing"
98
)
109

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.
1613
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.
1715
if err != nil {
18-
t.Fatal(err)
16+
fmt.Println(err)
17+
return
1918
}
2019

20+
// Get the Content-Type header from the response and convert it to lowercase.
2121
ct := strings.ToLower(response.Header.Get("Content-Type"))
22+
// If the Content-Type is not "text/event-stream", print a message and continue.
2223
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)
2425
}
2526

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
2729

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.
3035
if msg != nil {
3136
messages = append(messages, *msg)
3237
}
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))
3440
})
41+
// If an error occurs while reading the SSE messages, print the error and return from the function.
3542
if err != nil {
36-
t.Fatal(err)
43+
fmt.Println(err)
44+
return
3745
}
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))
3948
}
4049

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.
4653
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.
4755
if err != nil {
48-
t.Fatal(err)
56+
fmt.Println(err)
57+
return
4958
}
50-
defer safeClose(response.Body) //don't forget to close the response body
5159

60+
// Get the Content-Type header from the response and convert it to lowercase.
5261
ct := strings.ToLower(response.Header.Get("Content-Type"))
62+
// If the Content-Type is not "text/event-stream", print a message and continue.
5363
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)
5565
}
5666

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.
5873
if err != nil {
59-
t.Fatal(err)
74+
fmt.Println(err)
75+
return
6076
}
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.
6280
for msg := range channel {
81+
// If the message is not nil, append it to the messages slice.
6382
if msg != nil {
6483
messages = append(messages, *msg)
6584
}
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))
7587
}
88+
// Print the number of messages read.
89+
fmt.Printf("length of messages is %d", len(messages))
7690
}

readme.md

Lines changed: 2 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -16,88 +16,8 @@ download the library using
1616

1717
simple examples of how to use the library.
1818

19-
1. [example_test.go read by callback](/mojocn/sseread/blob/f8002c7d9655939755935a4ff143e01c8a67f583/example_test.go#L15)
20-
2. [example_test.go read by channel](/mojocn/sseread/blob/f8002c7d9655939755935a4ff143e01c8a67f583/example_test.go#L45)
21-
22-
```go
23-
//sseread/example_test.go
24-
import (
25-
"io"
26-
"log"
27-
"net/http"
28-
"strings"
29-
"testing"
30-
)
31-
32-
// TestRead is a test function for the Read function in the sseread package.
33-
// It sends a GET request to the specified URL and reads the response body as Server-Sent Events.
34-
// For each event, it appends it to the messages slice and logs the event ID, event type, and event data.
35-
// If an error occurs during the GET request or the reading of the events, it fails the test.
36-
func TestRead(t *testing.T) {
37-
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
38-
if err != nil {
39-
t.Fatal(err)
40-
}
41-
42-
ct := strings.ToLower(response.Header.Get("Content-Type"))
43-
if !strings.Contains(ct, "text/event-stream") {
44-
t.Fatal("expect content-type: text/event-stream, but actual", ct)
45-
}
46-
47-
defer safeClose(response.Body) //don't forget to close the response body
48-
49-
var messages []Event
50-
err = Read(response.Body, func(msg *Event) {
51-
if msg != nil {
52-
messages = append(messages, *msg)
53-
}
54-
t.Log(msg.ID, msg.Event, string(msg.Data))
55-
})
56-
if err != nil {
57-
t.Fatal(err)
58-
}
59-
t.Logf("length of messages is %d", len(messages))
60-
}
61-
62-
// TestReadChannel is a test function for the ReadCh function in the sseread package.
63-
// It sends a GET request to the specified URL and reads the response body as Server-Sent Events.
64-
// For each event, it logs the event ID, event type, and event data.
65-
// If an error occurs during the GET request or the reading of the events, it fails the test.
66-
func TestReadChannel(t *testing.T) {
67-
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
68-
if err != nil {
69-
t.Fatal(err)
70-
}
71-
defer safeClose(response.Body) //don't forget to close the response body
72-
73-
ct := strings.ToLower(response.Header.Get("Content-Type"))
74-
if !strings.Contains(ct, "text/event-stream") {
75-
t.Fatal("expect content-type: text/event-stream, but actual", ct)
76-
}
77-
78-
channel, err := ReadCh(response.Body)
79-
if err != nil {
80-
t.Fatal(err)
81-
}
82-
var messages []Event
83-
for msg := range channel {
84-
if msg != nil {
85-
messages = append(messages, *msg)
86-
}
87-
t.Log(msg.ID, msg.Event, string(msg.Data))
88-
}
89-
t.Logf("length of messages is %d", len(messages))
90-
91-
}
92-
93-
func safeClose(closer io.Closer) {
94-
if err := closer.Close(); err != nil {
95-
log.Println("error closing:", err)
96-
}
97-
}
98-
```
99-
100-
19+
1. [read SSE by callback](https://pkg.go.dev/github.com/mojocn/sseread#example-Read)
20+
2. [read SSE by channel](https://pkg.go.dev/github.com/mojocn/sseread#example-ReadCh)
10121

10222

10323
## Testing

sseread_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package sseread
2+
3+
import (
4+
"io"
5+
"log"
6+
"net/http"
7+
"strings"
8+
"testing"
9+
)
10+
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) {
16+
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
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
21+
ct := strings.ToLower(response.Header.Get("Content-Type"))
22+
if !strings.Contains(ct, "text/event-stream") {
23+
t.Fatal("expect content-type: text/event-stream, but actual", ct)
24+
}
25+
26+
defer safeClose(response.Body) //don't forget to close the response body
27+
28+
var messages []Event
29+
err = Read(response.Body, func(msg *Event) {
30+
if msg != nil {
31+
messages = append(messages, *msg)
32+
}
33+
t.Log(msg.ID, msg.Event, string(msg.Data))
34+
})
35+
if err != nil {
36+
t.Fatal(err)
37+
}
38+
t.Logf("length of messages is %d", len(messages))
39+
}
40+
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) {
46+
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
47+
if err != nil {
48+
t.Fatal(err)
49+
}
50+
defer safeClose(response.Body) //don't forget to close the response body
51+
52+
ct := strings.ToLower(response.Header.Get("Content-Type"))
53+
if !strings.Contains(ct, "text/event-stream") {
54+
t.Fatal("expect content-type: text/event-stream, but actual", ct)
55+
}
56+
57+
channel, err := ReadCh(response.Body)
58+
if err != nil {
59+
t.Fatal(err)
60+
}
61+
var messages []Event
62+
for msg := range channel {
63+
if msg != nil {
64+
messages = append(messages, *msg)
65+
}
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)
75+
}
76+
}

0 commit comments

Comments
 (0)