Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0bba5cd

Browse files
committedNov 4, 2022
Fixed missing media type in B output
1 parent 51281dc commit 0bba5cd

File tree

3 files changed

+297
-2
lines changed

3 files changed

+297
-2
lines changed
 

‎bob.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ func (b *B) FromTape(tape bob.Tape) (err error) {
5656
// Media type is after data
5757
b.MediaType = tape.Cell[startIndex+2].S
5858

59-
// Encoding is after media
60-
b.Encoding = tape.Cell[startIndex+3].S
59+
// Optional Encoding is after media
60+
if len(tape.Cell) > startIndex+3 && tape.Cell[startIndex+3].S != "" {
61+
b.Encoding = tape.Cell[startIndex+3].S
62+
} else {
63+
// default encoding is binary
64+
b.Encoding = string(EncodingBinary)
65+
}
6166

6267
switch EncodingType(strings.ToLower(b.Encoding)) {
6368
case EncodingGzip:

‎bob_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,40 @@ func TestNewFromTapes(t *testing.T) {
8080

8181
// todo: finish tests and examples (need BOB tx)
8282
}
83+
84+
// TestNewFromTapes tests for nil case in NewFromTapes()
85+
func TestNewFromTape2(t *testing.T) {
86+
87+
expectedTx := "8216e4be2e93dc90c561ce03b25338756efb80cbf6fda46c9df932696a2f5814"
88+
89+
tx, err := bob.NewFromString(exampleBobTx2)
90+
if err != nil {
91+
t.Fatalf("error occurred: %s", err.Error())
92+
}
93+
if tx.Tx.H != expectedTx {
94+
t.Fatalf("expected Tx.h: %s got: %s", expectedTx, tx.Tx.H)
95+
}
96+
97+
b := &B{}
98+
err = b.FromTape(tx.Out[0].Tape[1])
99+
if err != nil {
100+
t.Fatalf("error occurred: %s", err.Error())
101+
}
102+
if b.Encoding != "UTF-8" {
103+
t.Fatalf("expected Encoding: UTF-8 got: %s", b.Encoding)
104+
}
105+
if b.MediaType != "text/markdown" {
106+
t.Fatalf("expected MediaType: text/markdown got: %s", b.Encoding)
107+
}
108+
109+
err = b.FromTape(tx.Out[0].Tape[2])
110+
if err != nil {
111+
t.Fatalf("error occurred: %s", err.Error())
112+
}
113+
if b.Encoding != "binary" {
114+
t.Fatalf("expected Encoding: binary got: %s", b.Encoding)
115+
}
116+
if b.MediaType != "image/png" {
117+
t.Fatalf("expected MediaType: image/png got: %s", b.Encoding)
118+
}
119+
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Please sign in to comment.