MCPcopy
hub / github.com/nats-io/nats.go / TestJetStreamConvertDirectMsgResponseToMsg

Function TestJetStreamConvertDirectMsgResponseToMsg

js_test.go:25–93  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

23)
24
25func TestJetStreamConvertDirectMsgResponseToMsg(t *testing.T) {
26 // This test checks the conversion of a "direct get message" response
27 // to a JS message based on the content of specific NATS headers.
28 // It is very specific to the order headers retrieval is made in
29 // convertDirectGetMsgResponseToMsg(), so it may need adjustment
30 // if changes are made there.
31
32 msg := NewMsg("inbox")
33
34 check := func(errTxt string) {
35 t.Helper()
36 m, err := convertDirectGetMsgResponseToMsg("test", msg)
37 if err == nil || !strings.Contains(err.Error(), errTxt) {
38 t.Fatalf("Expected error contain %q, got %v", errTxt, err)
39 }
40 if m != nil {
41 t.Fatalf("Expected nil message, got %v", m)
42 }
43 }
44
45 check("should have headers")
46
47 msg.Header.Set(statusHdr, noMessagesSts)
48 check(ErrMsgNotFound.Error())
49
50 msg.Header.Set(statusHdr, reqTimeoutSts)
51 check("unable to get message")
52
53 msg.Header.Set(descrHdr, "some error text")
54 check("some error text")
55
56 msg.Header.Del(statusHdr)
57 msg.Header.Del(descrHdr)
58 msg.Header.Set("some", "header")
59 check("missing stream")
60
61 msg.Header.Set(JSStream, "test")
62 check("missing sequence")
63
64 msg.Header.Set(JSSequence, "abc")
65 check("invalid sequence")
66
67 msg.Header.Set(JSSequence, "1")
68 check("missing timestamp")
69
70 msg.Header.Set(JSTimeStamp, "aaaaaaaaa bbbbbbbbbbbb cccccccccc ddddddddddd eeeeeeeeee ffffff")
71 check("invalid timestamp")
72
73 msg.Header.Set(JSTimeStamp, "2006-01-02 15:04:05.999999999 +0000 UTC")
74 check("missing subject")
75
76 msg.Header.Set(JSSubject, "foo")
77 r, err := convertDirectGetMsgResponseToMsg("test", msg)
78 if err != nil {
79 t.Fatalf("Error during convert: %v", err)
80 }
81 if r.Subject != "foo" {
82 t.Fatalf("Expected subject to be 'foo', got %q", r.Subject)

Callers

nothing calls this directly

Calls 7

NewMsgFunction · 0.85
FatalfMethod · 0.80
ErrorMethod · 0.65
SetMethod · 0.65
DelMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected