Skip to content

Commit 109f73b

Browse files
author
zhuxiaoyang
committed
add ut about create & update content with gitlab
Signed-off-by: zhuxiaoyang <[email protected]>
1 parent 27f35ef commit 109f73b

File tree

4 files changed

+74
-8
lines changed

4 files changed

+74
-8
lines changed

scm/driver/gitlab/content_test.go

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,64 @@ func TestContentFind(t *testing.T) {
5353
}
5454

5555
func TestContentCreate(t *testing.T) {
56-
content := new(contentService)
57-
_, err := content.Create(context.Background(), "octocat/hello-world", "README", nil)
58-
if err != scm.ErrNotSupported {
59-
t.Errorf("Expect Not Supported error")
56+
defer gock.Off()
57+
58+
gock.New("https://gitlab.com").
59+
Post("/api/v4/projects/diaspora/diaspora/repository/files/app/project.rb").
60+
Reply(201).
61+
Type("application/json").
62+
SetHeaders(mockHeaders).
63+
File("testdata/content_create.json")
64+
65+
client := NewDefault()
66+
params := &scm.ContentParams{
67+
Message: "create a new file",
68+
Data: []byte("bXkgbmV3IGZpbGUgY29udGVudHM="),
69+
Signature: scm.Signature{
70+
Name: "Firstname Lastname",
71+
72+
},
73+
}
74+
75+
res, err := client.Contents.Create(context.Background(), "diaspora/diaspora", "app/project.rb", params)
76+
if err != nil {
77+
t.Error(err)
78+
return
79+
}
80+
81+
if res.Status != 201 {
82+
t.Errorf("Unexpected Results")
6083
}
6184
}
6285

6386
func TestContentUpdate(t *testing.T) {
64-
content := new(contentService)
65-
_, err := content.Update(context.Background(), "octocat/hello-world", "README", nil)
66-
if err != scm.ErrNotSupported {
67-
t.Errorf("Expect Not Supported error")
87+
defer gock.Off()
88+
89+
gock.New("https://gitlab.com").
90+
Put("/api/v4/projects/diaspora/diaspora/repository/files/app/project.rb").
91+
Reply(200).
92+
Type("application/json").
93+
SetHeaders(mockHeaders).
94+
File("testdata/content_update.json")
95+
96+
client := NewDefault()
97+
params := &scm.ContentParams{
98+
Message: "update file",
99+
Data: []byte("bXkgbmV3IGZpbGUgY29udGVudHM="),
100+
Signature: scm.Signature{
101+
Name: "Firstname Lastname",
102+
103+
},
104+
}
105+
106+
res, err := client.Contents.Update(context.Background(), "diaspora/diaspora", "app/project.rb", params)
107+
if err != nil {
108+
t.Error(err)
109+
return
110+
}
111+
112+
if res.Status != 200 {
113+
t.Errorf("Unexpected Results")
68114
}
69115
}
70116

scm/driver/gitlab/gitlab.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package gitlab
77

88
import (
9+
"bytes"
910
"context"
1011
"encoding/json"
1112
"net/url"
@@ -62,6 +63,17 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface
6263
Path: path,
6364
}
6465

66+
// if we are posting or putting data, we need to
67+
// write it to the body of the request.
68+
if in != nil {
69+
buf := new(bytes.Buffer)
70+
json.NewEncoder(buf).Encode(in)
71+
req.Header = map[string][]string{
72+
"Content-Type": {"application/json"},
73+
}
74+
req.Body = buf
75+
}
76+
6577
// execute the http request
6678
res, err := c.Client.Do(ctx, req)
6779
if err != nil {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"file_path": "app/project.rb",
3+
"branch": "master"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"file_path": "app/project.rb",
3+
"branch": "master"
4+
}

0 commit comments

Comments
 (0)