-
Notifications
You must be signed in to change notification settings - Fork 3
/
server_test.go
60 lines (48 loc) · 1.54 KB
/
server_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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo"
)
type spaceAPIValidatorFormat struct {
Data string `json:"data"`
}
func TestSpaceApi(t *testing.T) {
// Setup
e := echo.New()
req := httptest.NewRequest(echo.GET, "/api/v2.0/SpaceAPI", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
spaceAPIRouteHandler(c)
if http.StatusOK != rec.Code {
t.Errorf("expected status OK, got %v", rec.Code)
}
bodyBytes, err := ioutil.ReadAll(rec.Body)
if err != nil {
t.Errorf("Couldn't read Space Api response. Error: %v", err)
}
var spaceDescriptor SpaceDescriptor
err = json.Unmarshal(bodyBytes, &spaceDescriptor)
if err != nil {
t.Errorf("Responce from Space Api isn't couldn't be assigned to SpaceDescriptor. Error: %v, %v", err, &spaceDescriptor)
}
spaceDescriptorStringified, err := json.Marshal(&spaceDescriptor)
if err != nil {
t.Errorf("Got error while trying to convert spaceDescriptor to json . Error: %v", err)
}
requestData, err := json.Marshal(spaceAPIValidatorFormat{string(spaceDescriptorStringified)})
if err != nil {
t.Errorf("Got error while trying to create request for spaceDescriptor. Error: %v", err)
}
res, err := http.Post("https://validator.spacedirectory.org/v1/validate/", "application/json", bytes.NewBuffer(requestData))
if res.StatusCode != http.StatusOK {
t.Errorf("expected status OK, got %v", res.StatusCode)
}
if err != nil {
t.Errorf("Got error while trying to validate spaceDescriptor. Error: %v", err)
}
}