(t *testing.T)
| 113 | } |
| 114 | |
| 115 | func TestReadMessage(t *testing.T) { |
| 116 | t.Run("simple query message", func(t *testing.T) { |
| 117 | var buf bytes.Buffer |
| 118 | |
| 119 | query := "SELECT 1" |
| 120 | // Query message: 'Q' + length + query + null |
| 121 | buf.WriteByte('Q') |
| 122 | _ = binary.Write(&buf, binary.BigEndian, int32(len(query)+5)) // length includes itself and null |
| 123 | buf.WriteString(query) |
| 124 | buf.WriteByte(0) |
| 125 | |
| 126 | msgType, body, err := wire.ReadMessage(&buf) |
| 127 | if err != nil { |
| 128 | t.Fatalf("wire.ReadMessage() error = %v", err) |
| 129 | } |
| 130 | |
| 131 | if msgType != 'Q' { |
| 132 | t.Errorf("msgType = %c, want Q", msgType) |
| 133 | } |
| 134 | |
| 135 | // Body includes the null terminator |
| 136 | expectedBody := query + "\x00" |
| 137 | if string(body) != expectedBody { |
| 138 | t.Errorf("body = %q, want %q", string(body), expectedBody) |
| 139 | } |
| 140 | }) |
| 141 | |
| 142 | t.Run("terminate message", func(t *testing.T) { |
| 143 | var buf bytes.Buffer |
| 144 | |
| 145 | // Terminate message: 'X' + length (4, just the length itself) |
| 146 | buf.WriteByte('X') |
| 147 | _ = binary.Write(&buf, binary.BigEndian, int32(4)) |
| 148 | |
| 149 | msgType, body, err := wire.ReadMessage(&buf) |
| 150 | if err != nil { |
| 151 | t.Fatalf("wire.ReadMessage() error = %v", err) |
| 152 | } |
| 153 | |
| 154 | if msgType != 'X' { |
| 155 | t.Errorf("msgType = %c, want X", msgType) |
| 156 | } |
| 157 | |
| 158 | if len(body) != 0 { |
| 159 | t.Errorf("body length = %d, want 0", len(body)) |
| 160 | } |
| 161 | }) |
| 162 | |
| 163 | t.Run("message with binary data", func(t *testing.T) { |
| 164 | var buf bytes.Buffer |
| 165 | |
| 166 | data := []byte{0x01, 0x02, 0x03, 0x00, 0xFF} |
| 167 | buf.WriteByte('d') // CopyData |
| 168 | _ = binary.Write(&buf, binary.BigEndian, int32(len(data)+4)) |
| 169 | buf.Write(data) |
| 170 | |
| 171 | msgType, body, err := wire.ReadMessage(&buf) |
| 172 | if err != nil { |
nothing calls this directly
no test coverage detected