aboutsummaryrefslogtreecommitdiffstats
path: root/formats/packp/ulreq/ulreq_test.go
diff options
context:
space:
mode:
authorAlberto Cortés <alcortesm@gmail.com>2016-11-02 10:14:15 +0100
committerMáximo Cuadros <mcuadros@gmail.com>2016-11-02 09:14:15 +0000
commit1918bfe458729488e4a656cbbef7a2430e7ec2f5 (patch)
treed130d025584f3db0f781028db8978bb0a6a227ef /formats/packp/ulreq/ulreq_test.go
parent6b7464a22c6177d9e0cf96e1aaaae13c127c3149 (diff)
downloadgo-git-1918bfe458729488e4a656cbbef7a2430e7ec2f5.tar.gz
ulreq: adds encoder and decoder for upload-request messages (#106)
* ulreq: adds encoder and decoder for upload-request messages * ulreq: stop using _test suffix for testing package names (@mcuadros comment)
Diffstat (limited to 'formats/packp/ulreq/ulreq_test.go')
-rw-r--r--formats/packp/ulreq/ulreq_test.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/formats/packp/ulreq/ulreq_test.go b/formats/packp/ulreq/ulreq_test.go
new file mode 100644
index 0000000..2c5e85a
--- /dev/null
+++ b/formats/packp/ulreq/ulreq_test.go
@@ -0,0 +1,91 @@
+package ulreq
+
+import (
+ "fmt"
+ "os"
+ "strings"
+ "testing"
+ "time"
+
+ "gopkg.in/src-d/go-git.v4/core"
+ "gopkg.in/src-d/go-git.v4/formats/packp/pktline"
+
+ . "gopkg.in/check.v1"
+)
+
+func Test(t *testing.T) { TestingT(t) }
+
+func ExampleEncoder_Encode() {
+ // Create an empty UlReq with the contents you want...
+ ur := New()
+
+ // Add a couple of wants
+ ur.Wants = append(ur.Wants, core.NewHash("3333333333333333333333333333333333333333"))
+ ur.Wants = append(ur.Wants, core.NewHash("1111111111111111111111111111111111111111"))
+ ur.Wants = append(ur.Wants, core.NewHash("2222222222222222222222222222222222222222"))
+
+ // And some capabilities you will like the server to use
+ ur.Capabilities.Add("sysref", "HEAD:/refs/heads/master")
+ ur.Capabilities.Add("ofs-delta")
+
+ // Add a couple of shallows
+ ur.Shallows = append(ur.Shallows, core.NewHash("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"))
+ ur.Shallows = append(ur.Shallows, core.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
+
+ // And retrict the answer of the server to commits newer than "2015-01-02 03:04:05 UTC"
+ since := time.Date(2015, time.January, 2, 3, 4, 5, 0, time.UTC)
+ ur.Depth = DepthSince(since)
+
+ // Create a new Encode for the stdout...
+ e := NewEncoder(os.Stdout)
+ // ...and encode the upload-request to it.
+ _ = e.Encode(ur) // ignoring errors for brevity
+ // Output:
+ // 005bwant 1111111111111111111111111111111111111111 ofs-delta sysref=HEAD:/refs/heads/master
+ // 0032want 2222222222222222222222222222222222222222
+ // 0032want 3333333333333333333333333333333333333333
+ // 0035shallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ // 0035shallow bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
+ // 001cdeepen-since 1420167845
+ // 0000
+}
+
+func ExampleDecoder_Decode() {
+ // Here is a raw advertised-ref message.
+ raw := "" +
+ "005bwant 1111111111111111111111111111111111111111 ofs-delta sysref=HEAD:/refs/heads/master\n" +
+ "0032want 2222222222222222222222222222222222222222\n" +
+ "0032want 3333333333333333333333333333333333333333\n" +
+ "0035shallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" +
+ "0035shallow bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" +
+ "001cdeepen-since 1420167845\n" + // 2015-01-02 03:04:05 +0000 UTC
+ pktline.FlushString
+
+ // Use the raw message as our input.
+ input := strings.NewReader(raw)
+
+ // Create the Decoder reading from our input.
+ d := NewDecoder(input)
+
+ // Decode the input into a newly allocated UlReq value.
+ ur := New()
+ _ = d.Decode(ur) // error check ignored for brevity
+
+ // Do something interesting with the UlReq, e.g. print its contents.
+ fmt.Println("capabilities =", ur.Capabilities.String())
+ fmt.Println("wants =", ur.Wants)
+ fmt.Println("shallows =", ur.Shallows)
+ switch depth := ur.Depth.(type) {
+ case DepthCommits:
+ fmt.Println("depth =", int(depth))
+ case DepthSince:
+ fmt.Println("depth =", time.Time(depth))
+ case DepthReference:
+ fmt.Println("depth =", string(depth))
+ }
+ // Output:
+ // capabilities = ofs-delta sysref=HEAD:/refs/heads/master
+ // wants = [1111111111111111111111111111111111111111 2222222222222222222222222222222222222222 3333333333333333333333333333333333333333]
+ // shallows = [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb]
+ // depth = 2015-01-02 03:04:05 +0000 UTC
+}