forked from anthonydahanne/stash
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgetrepository_test.go
More file actions
145 lines (136 loc) · 3.59 KB
/
Copy pathgetrepository_test.go
File metadata and controls
145 lines (136 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package stash
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
const response string = `
{
"link" : {
"rel" : "self",
"url" : "/projects/PROJ/repos/trunk/browse"
},
"project" : {
"link" : {
"rel" : "self",
"url" : "/projects/PROJ"
},
"name" : "PROJ Dev",
"isPersonal" : false,
"description" : "The PROJ stash.",
"key" : "PROJ",
"public" : true,
"id" : 107,
"type" : "NORMAL",
"links" : {
"self" : [
{
"href" : "http://example.com:8888/projects/PROJ"
}
]
}
},
"name" : "trunk",
"state" : "AVAILABLE",
"scmId" : "git",
"cloneUrl" : "http://user@example.com:8888/scm/PROJ/trunk.git",
"statusMessage" : "Available",
"public" : false,
"slug" : "trunk",
"id" : 536,
"forkable" : true,
"links" : {
"clone" : [
{
"href" : "ssh://git@example.com:9999/PROJ/trunk.git",
"name" : "ssh"
},
{
"href" : "http://user@example.com:8888/scm/PROJ/trunk.git",
"name" : "http"
}
],
"self" : [
{
"href" : "http://example.com:8888/projects/PROJ/repos/trunk/browse"
}
]
}
}
`
func TestGetRepository(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Fatalf("wanted GET but found %s\n", r.Method)
}
url := *r.URL
if url.Path != "/rest/api/1.0/projects/PROJ/repos/slug" {
t.Fatalf("GetBranches() URL path expected to be /rest/api/1.0/projects/PROJ/repos/slug but found %s\n", url.Path)
}
if r.Header.Get("Authorization") != "Basic dTpw" {
t.Fatalf("Want Basic dTpw but found %s\n", r.Header.Get("Authorization"))
}
fmt.Fprintln(w, response)
}))
defer testServer.Close()
url, _ := url.Parse(testServer.URL)
stashClient := NewClient("u", "p", url)
repo, err := stashClient.GetRepository("PROJ", "slug")
if err != nil {
t.Fatalf("Not expecting error: %v\n", err)
}
// spot checks
if repo.Slug != "trunk" {
t.Fatalf("Want trunk but got %s\n", repo.Slug)
}
if repo.ID != 536 {
t.Fatalf("Want 536 but got %d\n", repo.ID)
}
if url := repo.SshUrl(); url != "ssh://git@example.com:9999/PROJ/trunk.git" {
t.Fatalf("Want ssh://git@example.com:9999/PROJ/trunk.git but got %d\n", repo.ID)
}
}
func TestGetRepository404(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
w.Write([]byte(`{
"errors": [
{
"context": null,
"message": "A detailed error message.",
"exceptionName": null
}
]
}`))
}))
defer testServer.Close()
url, _ := url.Parse(testServer.URL)
stashClient := NewClient("u", "p", url)
_, err := stashClient.GetRepository("PROJ", "slug")
if err == nil {
t.Fatalf("Expecting error but did not get one\n")
}
}
func TestGetRepository401(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(401)
w.Write([]byte(`{
"errors": [
{
"context": null,
"message": "A detailed error message.",
"exceptionName": null
}
]
}`))
}))
defer testServer.Close()
url, _ := url.Parse(testServer.URL)
stashClient := NewClient("u", "p", url)
_, err := stashClient.GetRepository("PROJ", "slug")
if err == nil {
t.Fatalf("Expecting error but did not get one\n")
}
}