hnakra/protocol/message_http_test.go

143 lines
3.7 KiB
Go
Raw Normal View History

2023-05-25 16:08:56 -06:00
package protocol
import "bytes"
import "testing"
func TestMessageHTTPBodyEnd (test *testing.T) {
conduit := new(bytes.Buffer)
message := MessageHTTPBodyEnd {
ID: 493234,
}
message.Send(conduit)
ty, err := ReadType(conduit)
if err != nil { test.Fatal("when reading type code:", err) }
if ty != TypeHTTPBodyEnd { test.Fatal("expected", TypeHTTPBodyEnd, "got", ty) }
got, err := ReadMessageHTTPBodyEnd(conduit)
if err != nil { test.Fatal("when reading message:", err) }
equal := message.ID == got.ID
if !equal { test.Fatal("structures not equal") }
if conduit.Len() != 0 {
test.Fatal("buffer len", conduit.Len(), "instead of 0")
}
}
func TestMessageHTTPBodyRequest (test *testing.T) {
conduit := new(bytes.Buffer)
message := MessageHTTPBodyRequest {
ID: 493234,
}
message.Send(conduit)
ty, err := ReadType(conduit)
if err != nil { test.Fatal("when reading type code:", err) }
if ty != TypeHTTPBodyRequest { test.Fatal("expected", TypeHTTPBodyRequest, "got", ty) }
got, err := ReadMessageHTTPBodyRequest(conduit)
if err != nil { test.Fatal("when reading message:", err) }
equal := message.ID == got.ID
if !equal { test.Fatal("structures not equal") }
if conduit.Len() != 0 {
test.Fatal("buffer len", conduit.Len(), "instead of 0")
}
}
func TestMessageHTTPBodySegment (test *testing.T) {
conduit := new(bytes.Buffer)
message := MessageHTTPBodySegment {
ID: 22441,
Data: makeTestBuffer(10301),
}
message.Send(conduit)
ty, err := ReadType(conduit)
if err != nil { test.Fatal("when reading type code:", err) }
if ty != TypeHTTPBodySegment { test.Fatal("expected", TypeHTTPBodySegment, "got", ty) }
got, err := ReadMessageHTTPBodySegment(conduit)
if err != nil { test.Fatal("when reading message:", err) }
equal :=
message.ID == got.ID &&
sliceEqual(message.Data, got.Data)
if !equal { test.Fatal("structures not equal") }
if conduit.Len() != 0 {
test.Fatal("buffer len", conduit.Len(), "instead of 0")
}
}
func TestMessageHTTPRequest (test *testing.T) {
conduit := new(bytes.Buffer)
message := MessageHTTPRequest {
ID: 9994562,
Address: "some.thing",
Method: "GET",
URL: "https://example.com/hahaha",
Header: Map {
"Bird": []string { "Bird1", "Bird2" },
"Tree": []string { "Tree1" },
},
}
message.Send(conduit)
ty, err := ReadType(conduit)
if err != nil { test.Fatal("when reading type code:", err) }
if ty != TypeHTTPRequest { test.Fatal("expected", TypeHTTPRequest, "got", ty) }
got, err := ReadMessageHTTPRequest(conduit)
if err != nil { test.Fatal("when reading message:", err) }
equal :=
message.ID == got.ID &&
message.Address == got.Address &&
message.Method == got.Method &&
message.URL == got.URL &&
mapEqual(message.Header, got.Header)
if !equal { test.Fatal("structures not equal") }
if conduit.Len() != 0 {
test.Fatal("buffer len", conduit.Len(), "instead of 0")
}
}
func TestMessageHTTPResponse (test *testing.T) {
conduit := new(bytes.Buffer)
message := MessageHTTPResponse {
ID: 77777,
Status: 200,
Header: Map {
"Bird": []string { "Bird1", "Bird2" },
"Tree": []string { "Tree1" },
},
}
message.Send(conduit)
ty, err := ReadType(conduit)
if err != nil { test.Fatal("when reading type code:", err) }
if ty != TypeHTTPResponse { test.Fatal("expected", TypeHTTPResponse, "got", ty) }
got, err := ReadMessageHTTPResponse(conduit)
if err != nil { test.Fatal("when reading message:", err) }
equal :=
message.ID == got.ID &&
message.Status == got.Status &&
mapEqual(message.Header, got.Header)
if !equal { test.Fatal("structures not equal") }
if conduit.Len() != 0 {
test.Fatal("buffer len", conduit.Len(), "instead of 0")
}
}