Skip to content

Commit

Permalink
Fixed missing media type in B output
Browse files Browse the repository at this point in the history
  • Loading branch information
icellan committed Nov 4, 2022
1 parent 51281dc commit 0bba5cd
Show file tree
Hide file tree
Showing 3 changed files with 297 additions and 2 deletions.
9 changes: 7 additions & 2 deletions bob.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ func (b *B) FromTape(tape bob.Tape) (err error) {
// Media type is after data
b.MediaType = tape.Cell[startIndex+2].S

// Encoding is after media
b.Encoding = tape.Cell[startIndex+3].S
// Optional Encoding is after media
if len(tape.Cell) > startIndex+3 && tape.Cell[startIndex+3].S != "" {
b.Encoding = tape.Cell[startIndex+3].S
} else {
// default encoding is binary
b.Encoding = string(EncodingBinary)
}

switch EncodingType(strings.ToLower(b.Encoding)) {
case EncodingGzip:
Expand Down
37 changes: 37 additions & 0 deletions bob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,40 @@ func TestNewFromTapes(t *testing.T) {

// todo: finish tests and examples (need BOB tx)
}

// TestNewFromTapes tests for nil case in NewFromTapes()
func TestNewFromTape2(t *testing.T) {

expectedTx := "8216e4be2e93dc90c561ce03b25338756efb80cbf6fda46c9df932696a2f5814"

tx, err := bob.NewFromString(exampleBobTx2)
if err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
if tx.Tx.H != expectedTx {
t.Fatalf("expected Tx.h: %s got: %s", expectedTx, tx.Tx.H)
}

b := &B{}
err = b.FromTape(tx.Out[0].Tape[1])
if err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
if b.Encoding != "UTF-8" {
t.Fatalf("expected Encoding: UTF-8 got: %s", b.Encoding)
}
if b.MediaType != "text/markdown" {
t.Fatalf("expected MediaType: text/markdown got: %s", b.Encoding)
}

err = b.FromTape(tx.Out[0].Tape[2])
if err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
if b.Encoding != "binary" {
t.Fatalf("expected Encoding: binary got: %s", b.Encoding)
}
if b.MediaType != "image/png" {
t.Fatalf("expected MediaType: image/png got: %s", b.Encoding)
}
}
Loading

0 comments on commit 0bba5cd

Please sign in to comment.