Skip to content

Commit d9f7424

Browse files
committed
feat: add lab6
1 parent f79d5a2 commit d9f7424

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed

.github/workflows/lab6.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This workflow will build a golang project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: lab6
5+
6+
on:
7+
push:
8+
paths:
9+
- 'lab6/**'
10+
11+
jobs:
12+
13+
build:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- uses: actions/setup-go@v4
20+
with:
21+
go-version-file: 'lab6/go.mod'
22+
cache: false
23+
24+
- name: Run
25+
working-directory: 'lab6'
26+
run: go test

lab6/lab6.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
)
6+
7+
type Book struct {
8+
// TODO: Finish struct
9+
}
10+
11+
var bookshelf = []Book{
12+
// TODO: Init bookshelf
13+
}
14+
15+
func getBooks(c *gin.Context) {
16+
}
17+
func getBook(c *gin.Context) {
18+
}
19+
func addBook(c *gin.Context) {
20+
}
21+
func deleteBook(c *gin.Context) {
22+
}
23+
func updateBook(c *gin.Context) {
24+
}
25+
26+
func main() {
27+
r := gin.Default()
28+
r.RedirectFixedPath = true
29+
30+
// TODO: Add routes
31+
32+
err := r.Run(":8087")
33+
if err != nil {
34+
return
35+
}
36+
}

lab6/lab6_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"github.com/stretchr/testify/assert"
6+
"io"
7+
"net/http"
8+
"testing"
9+
"time"
10+
)
11+
12+
func TestRest(t *testing.T) {
13+
testData := []struct {
14+
path string
15+
method string
16+
body string
17+
status int
18+
expected string
19+
}{
20+
{"/bookshelf", http.MethodGet, "", 200, `[{"id":1,"name":"Blue Bird","pages":500}]`},
21+
{"/bookshelf/1", http.MethodGet, "", 200, `{"id":1,"name":"Blue Bird","pages":500}`},
22+
{"/bookshelf/2", http.MethodGet, "", 404, `{"message":"book not found"}`},
23+
{"/bookshelf", http.MethodPost, `{"name":"Blue Bird","pages":500}`, 409, `{"message":"duplicate book name"}`},
24+
{"/bookshelf", http.MethodPost, `{"name":"Red Bird","pages":500}`, 201, `{"id":2,"name":"Red Bird","pages":500}`},
25+
{"/bookshelf", http.MethodGet, "", 200, `[{"id":1,"name":"Blue Bird","pages":500},{"id":2,"name":"Red Bird","pages":500}]`},
26+
{"/bookshelf/2", http.MethodGet, "", 200, `{"id":2,"name":"Red Bird","pages":500}`},
27+
{"/bookshelf/2", http.MethodPut, `{"name":"Yellow Bird","pages":500}`, 200, `{"id":2,"name":"Yellow Bird","pages":500}`},
28+
{"/bookshelf/2", http.MethodGet, "", 200, `{"id":2,"name":"Yellow Bird","pages":500}`},
29+
{"/bookshelf/2", http.MethodPut, `{"name":"Blue Bird","pages":500}`, 409, `{"message":"duplicate book name"}`},
30+
{"/bookshelf/300", http.MethodPut, `{"name":"Real life","pages":500}`, 404, `{"message":"book not found"}`},
31+
{"/bookshelf/2", http.MethodDelete, "", 204, ``},
32+
{"/bookshelf/800", http.MethodDelete, "", 204, ``},
33+
{"/bookshelf/2", http.MethodGet, "", 404, `{"message":"book not found"}`},
34+
{"/bookshelf", http.MethodGet, "", 200, `[{"id":1,"name":"Blue Bird","pages":500}]`},
35+
{"/bookshelf", http.MethodPost, `{"name":"Les Misérables","pages":500}`, 201, `{"id":3,"name":"Les Misérables","pages":500}`},
36+
{"/bookshelf", http.MethodGet, "", 200, `[{"id":1,"name":"Blue Bird","pages":500},{"id":3,"name":"Les Misérables","pages":500}]`},
37+
{"/bookshelf/3", http.MethodGet, "", 200, `{"id":3,"name":"Les Misérables","pages":500}`},
38+
{"/bookshelf/3", http.MethodPut, `{"name":"Red Bird","pages":1000}`, 200, `{"id":3,"name":"Red Bird","pages":1000}`},
39+
{"/bookshelf/3", http.MethodGet, "", 200, `{"id":3,"name":"Red Bird","pages":1000}`},
40+
{"/bookshelf/3", http.MethodDelete, "", 204, ``},
41+
{"/bookshelf/2", http.MethodDelete, "", 204, ``},
42+
{"/bookshelf/1", http.MethodDelete, "", 204, ``},
43+
{"/bookshelf", http.MethodGet, "", 200, `[]`},
44+
}
45+
46+
go func() {
47+
main()
48+
}()
49+
50+
isUp := false
51+
for i := 0; i < 30; i++ {
52+
_, err := http.Get("http://localhost:8087")
53+
if err == nil {
54+
isUp = true
55+
break
56+
}
57+
t.Logf("Server not up yet... try again")
58+
time.Sleep(2 * time.Second)
59+
}
60+
if !isUp {
61+
t.Fatal("Unable to connect to server")
62+
return
63+
}
64+
65+
for _, tc := range testData {
66+
bodyVal := tc.body
67+
if bodyVal == "" {
68+
bodyVal = "nil"
69+
}
70+
t.Logf("Sending request... Path: \"%s\"\tMethod: %s\tBody: %s", tc.path, tc.method, bodyVal)
71+
72+
req, err := http.NewRequest(tc.method, "http://localhost:8087"+tc.path, bytes.NewReader([]byte(tc.body)))
73+
if err != nil {
74+
t.Fatal(err)
75+
return
76+
}
77+
resp, err := http.DefaultClient.Do(req)
78+
if err != nil {
79+
t.Fatal(err)
80+
return
81+
}
82+
83+
if !assert.Equal(t, tc.status, resp.StatusCode) {
84+
return
85+
}
86+
87+
body, err := io.ReadAll(resp.Body)
88+
if err != nil {
89+
t.Fatal(err)
90+
return
91+
}
92+
if !assert.Equal(t, tc.expected, string(body)) {
93+
return
94+
}
95+
96+
err = resp.Body.Close()
97+
if err != nil {
98+
t.Fatal(err)
99+
return
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)