Skip to content

Commit

Permalink
feat: add lab6
Browse files Browse the repository at this point in the history
  • Loading branch information
AxelHowe committed Nov 11, 2024
1 parent f79d5a2 commit d9f7424
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/lab6.yml
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
36 changes: 36 additions & 0 deletions lab6/lab6.go
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
}
}
102 changes: 102 additions & 0 deletions lab6/lab6_test.go
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
}
}
}

0 comments on commit d9f7424

Please sign in to comment.