This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
forked from datacontract/datacontract-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen_test.go
75 lines (70 loc) · 1.92 KB
/
open_test.go
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
package datacontract
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"reflect"
"testing"
)
func Test_createDataContractInStudio(t *testing.T) {
dataContractStudioId := "my-data-contract-id"
dataContract, _ := os.ReadFile("test_resources/open/datacontract.yaml")
studioMock := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.String() {
case "/new":
if r.Method == http.MethodPost {
redirectUrl := fmt.Sprintf("%v/%v/edit", r.URL.Host, dataContractStudioId)
http.Redirect(w, r, redirectUrl, http.StatusFound)
return
}
case fmt.Sprintf("/%v/edit", dataContractStudioId):
if r.Method == http.MethodGet {
w.WriteHeader(http.StatusCreated)
return
}
case fmt.Sprintf("/%v/save", dataContractStudioId):
if r.Method == http.MethodPost &&
r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" &&
r.FormValue("yaml") == string(dataContract) {
w.WriteHeader(http.StatusNoContent)
return
}
default:
w.WriteHeader(http.StatusBadRequest)
}
}))
defer studioMock.Close()
type args struct {
dataContractFileName string
dataContractStudioUrl string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "open",
args: args{
dataContractFileName: "test_resources/open/datacontract.yaml",
dataContractStudioUrl: studioMock.URL,
},
want: fmt.Sprintf("%v/%v", studioMock.URL, dataContractStudioId),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := createDataContractInStudio(tt.args.dataContractFileName, tt.args.dataContractStudioUrl)
if (err != nil) != tt.wantErr {
t.Errorf("createDataContractInStudio() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("createDataContractInStudio() got = %v, want %v", got, tt.want)
}
})
}
}