| 1 |
package packp |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
func TestEncodeEmptyGitProtoRequest(t *testing.T) { |
| 9 |
var buf bytes.Buffer |
| 10 |
var p GitProtoRequest |
| 11 |
err := p.Encode(&buf) |
| 12 |
if err == nil { |
| 13 |
t.Fatal("expected error") |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
func TestEncodeGitProtoRequest(t *testing.T) { |
| 18 |
var buf bytes.Buffer |
| 19 |
p := GitProtoRequest{ |
| 20 |
RequestCommand: "command", |
| 21 |
Pathname: "pathname", |
| 22 |
Host: "host", |
| 23 |
ExtraParams: []string{"param1", "param2"}, |
| 24 |
} |
| 25 |
err := p.Encode(&buf) |
| 26 |
if err != nil { |
| 27 |
t.Fatal(err) |
| 28 |
} |
| 29 |
expected := "002ecommand pathname\x00host=host\x00\x00param1\x00param2\x00" |
| 30 |
if buf.String() != expected { |
| 31 |
t.Fatalf("expected %q, got %q", expected, buf.String()) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
func TestEncodeInvalidGitProtoRequest(t *testing.T) { |
| 36 |
var buf bytes.Buffer |
| 37 |
p := GitProtoRequest{ |
| 38 |
RequestCommand: "command", |
| 39 |
} |
| 40 |
err := p.Encode(&buf) |
| 41 |
if err == nil { |
| 42 |
t.Fatal("expected error") |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func TestDecodeEmptyGitProtoRequest(t *testing.T) { |
| 47 |
var buf bytes.Buffer |
| 48 |
var p GitProtoRequest |
| 49 |
err := p.Decode(&buf) |
| 50 |
if err == nil { |
| 51 |
t.Fatal("expected error") |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func TestDecodeGitProtoRequest(t *testing.T) { |
| 56 |
var buf bytes.Buffer |
| 57 |
buf.WriteString("002ecommand pathname\x00host=host\x00\x00param1\x00param2\x00") |
| 58 |
var p GitProtoRequest |
| 59 |
err := p.Decode(&buf) |
| 60 |
if err != nil { |
| 61 |
t.Fatal(err) |
| 62 |
} |
| 63 |
expected := GitProtoRequest{ |
| 64 |
RequestCommand: "command", |
| 65 |
Pathname: "pathname", |
| 66 |
Host: "host", |
| 67 |
ExtraParams: []string{"param1", "param2"}, |
| 68 |
} |
| 69 |
if p.RequestCommand != expected.RequestCommand { |
| 70 |
t.Fatalf("expected %q, got %q", expected.RequestCommand, p.RequestCommand) |
| 71 |
} |
| 72 |
if p.Pathname != expected.Pathname { |
| 73 |
t.Fatalf("expected %q, got %q", expected.Pathname, p.Pathname) |
| 74 |
} |
| 75 |
if p.Host != expected.Host { |
| 76 |
t.Fatalf("expected %q, got %q", expected.Host, p.Host) |
| 77 |
} |
| 78 |
if len(p.ExtraParams) != len(expected.ExtraParams) { |
| 79 |
t.Fatalf("expected %d, got %d", len(expected.ExtraParams), len(p.ExtraParams)) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func TestDecodeInvalidGitProtoRequest(t *testing.T) { |
| 84 |
var buf bytes.Buffer |
| 85 |
buf.WriteString("0026command \x00host=host\x00\x00param1\x00param2") |
| 86 |
var p GitProtoRequest |
| 87 |
err := p.Decode(&buf) |
| 88 |
if err == nil { |
| 89 |
t.Fatal("expected error") |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func TestValidateEmptyGitProtoRequest(t *testing.T) { |
| 94 |
var p GitProtoRequest |
| 95 |
err := p.validate() |
| 96 |
if err == nil { |
| 97 |
t.Fatal("expected error") |
| 98 |
} |
| 99 |
} |