-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# This workflow will build a golang project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go | ||
|
||
name: lab6 | ||
|
||
on: | ||
push: | ||
paths: | ||
- 'lab6/**' | ||
|
||
jobs: | ||
|
||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version-file: 'lab6/go.mod' | ||
cache: false | ||
|
||
- name: Run | ||
working-directory: 'lab6' | ||
run: go test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type Book struct { | ||
// TODO: Finish struct | ||
} | ||
|
||
var bookshelf = []Book{ | ||
// TODO: Init bookshelf | ||
} | ||
|
||
func getBooks(c *gin.Context) { | ||
} | ||
func getBook(c *gin.Context) { | ||
} | ||
func addBook(c *gin.Context) { | ||
} | ||
func deleteBook(c *gin.Context) { | ||
} | ||
func updateBook(c *gin.Context) { | ||
} | ||
|
||
func main() { | ||
r := gin.Default() | ||
r.RedirectFixedPath = true | ||
|
||
// TODO: Add routes | ||
|
||
err := r.Run(":8087") | ||
if err != nil { | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"github.com/stretchr/testify/assert" | ||
"io" | ||
"net/http" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestRest(t *testing.T) { | ||
testData := []struct { | ||
path string | ||
method string | ||
body string | ||
status int | ||
expected string | ||
}{ | ||
{"/bookshelf", http.MethodGet, "", 200, `[{"id":1,"name":"Blue Bird","pages":500}]`}, | ||
{"/bookshelf/1", http.MethodGet, "", 200, `{"id":1,"name":"Blue Bird","pages":500}`}, | ||
{"/bookshelf/2", http.MethodGet, "", 404, `{"message":"book not found"}`}, | ||
{"/bookshelf", http.MethodPost, `{"name":"Blue Bird","pages":500}`, 409, `{"message":"duplicate book name"}`}, | ||
{"/bookshelf", http.MethodPost, `{"name":"Red Bird","pages":500}`, 201, `{"id":2,"name":"Red Bird","pages":500}`}, | ||
{"/bookshelf", http.MethodGet, "", 200, `[{"id":1,"name":"Blue Bird","pages":500},{"id":2,"name":"Red Bird","pages":500}]`}, | ||
{"/bookshelf/2", http.MethodGet, "", 200, `{"id":2,"name":"Red Bird","pages":500}`}, | ||
{"/bookshelf/2", http.MethodPut, `{"name":"Yellow Bird","pages":500}`, 200, `{"id":2,"name":"Yellow Bird","pages":500}`}, | ||
{"/bookshelf/2", http.MethodGet, "", 200, `{"id":2,"name":"Yellow Bird","pages":500}`}, | ||
{"/bookshelf/2", http.MethodPut, `{"name":"Blue Bird","pages":500}`, 409, `{"message":"duplicate book name"}`}, | ||
{"/bookshelf/300", http.MethodPut, `{"name":"Real life","pages":500}`, 404, `{"message":"book not found"}`}, | ||
{"/bookshelf/2", http.MethodDelete, "", 204, ``}, | ||
{"/bookshelf/800", http.MethodDelete, "", 204, ``}, | ||
{"/bookshelf/2", http.MethodGet, "", 404, `{"message":"book not found"}`}, | ||
{"/bookshelf", http.MethodGet, "", 200, `[{"id":1,"name":"Blue Bird","pages":500}]`}, | ||
{"/bookshelf", http.MethodPost, `{"name":"Les Misérables","pages":500}`, 201, `{"id":3,"name":"Les Misérables","pages":500}`}, | ||
{"/bookshelf", http.MethodGet, "", 200, `[{"id":1,"name":"Blue Bird","pages":500},{"id":3,"name":"Les Misérables","pages":500}]`}, | ||
{"/bookshelf/3", http.MethodGet, "", 200, `{"id":3,"name":"Les Misérables","pages":500}`}, | ||
{"/bookshelf/3", http.MethodPut, `{"name":"Red Bird","pages":1000}`, 200, `{"id":3,"name":"Red Bird","pages":1000}`}, | ||
{"/bookshelf/3", http.MethodGet, "", 200, `{"id":3,"name":"Red Bird","pages":1000}`}, | ||
{"/bookshelf/3", http.MethodDelete, "", 204, ``}, | ||
{"/bookshelf/2", http.MethodDelete, "", 204, ``}, | ||
{"/bookshelf/1", http.MethodDelete, "", 204, ``}, | ||
{"/bookshelf", http.MethodGet, "", 200, `[]`}, | ||
} | ||
|
||
go func() { | ||
main() | ||
}() | ||
|
||
isUp := false | ||
for i := 0; i < 30; i++ { | ||
_, err := http.Get("http://localhost:8087") | ||
if err == nil { | ||
isUp = true | ||
break | ||
} | ||
t.Logf("Server not up yet... try again") | ||
time.Sleep(2 * time.Second) | ||
} | ||
if !isUp { | ||
t.Fatal("Unable to connect to server") | ||
return | ||
} | ||
|
||
for _, tc := range testData { | ||
bodyVal := tc.body | ||
if bodyVal == "" { | ||
bodyVal = "nil" | ||
} | ||
t.Logf("Sending request... Path: \"%s\"\tMethod: %s\tBody: %s", tc.path, tc.method, bodyVal) | ||
|
||
req, err := http.NewRequest(tc.method, "http://localhost:8087"+tc.path, bytes.NewReader([]byte(tc.body))) | ||
if err != nil { | ||
t.Fatal(err) | ||
return | ||
} | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
t.Fatal(err) | ||
return | ||
} | ||
|
||
if !assert.Equal(t, tc.status, resp.StatusCode) { | ||
return | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
t.Fatal(err) | ||
return | ||
} | ||
if !assert.Equal(t, tc.expected, string(body)) { | ||
return | ||
} | ||
|
||
err = resp.Body.Close() | ||
if err != nil { | ||
t.Fatal(err) | ||
return | ||
} | ||
} | ||
} |