Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Commit

Permalink
Feat/add web UI (#16)
Browse files Browse the repository at this point in the history
* Add tests
  • Loading branch information
ahelmy authored Jan 6, 2024
1 parent 35e98d3 commit a8af1b1
Show file tree
Hide file tree
Showing 3 changed files with 306 additions and 12 deletions.
2 changes: 0 additions & 2 deletions codecov.yml

This file was deleted.

20 changes: 10 additions & 10 deletions internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

const (
MainLayout = "layouts/main"

MainLayout = "layouts/main"
BasePath = "./ui"
JSONPath = "/json"
YAMLPath = "/yaml"
JWTPath = "/jwt"
Expand All @@ -20,8 +20,8 @@ const (
Base64Path = "/base64"
)

func newApp() *fiber.App {
engine := html.New("./ui", ".html")
func newApp(basePath string) *fiber.App {
engine := html.New(basePath, ".html")

return fiber.New(fiber.Config{
Views: engine,
Expand All @@ -31,8 +31,8 @@ func newApp() *fiber.App {
}

func StartServer(port int32) {
app := newApp()
defineResources(app)
app := newApp(BasePath)
defineResources(app, BasePath)
indexPage(app)
jsonPage(app)
uuidPage(app)
Expand Down Expand Up @@ -225,8 +225,8 @@ func base64Page(app *fiber.App) {
})
}

func defineResources(app *fiber.App) {
app.Static("/css", "./ui/css")
app.Static("/js", "./ui/js")
app.Static("/img", "./ui/img")
func defineResources(app *fiber.App, basePath string) {
app.Static("/css", basePath+"/css")
app.Static("/js", basePath+"/js")
app.Static("/img", basePath+"/img")
}
296 changes: 296 additions & 0 deletions internal/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
package internal

import (
"net/http"
"net/http/httptest"
"testing"
)

const (
BaseTestPath = "../ui"
)

func TestIndexPage(t *testing.T) {
app := newApp(BaseTestPath)
indexPage(app)
// Create a test request to the "/" route
req := httptest.NewRequest(http.MethodGet, "/", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// TODO: Add more assertions for the response body or other expectations
}

func TestJSONPage(t *testing.T) {
app := newApp(BaseTestPath)
jsonPage(app)
t.Run("Test JSON Page - Beautify", func(t *testing.T) {
// Create a test request to the "/json" route with action=beautify and json=...
req := httptest.NewRequest(http.MethodGet, "/json?action=beautify&json=...", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// TODO: Add assertions for the response body or other expectations
})

t.Run("Test JSON Page - Minify", func(t *testing.T) {
// Create a test request to the "/json" route with action=minify and json=...
req := httptest.NewRequest(http.MethodGet, "/json?action=minify&json=...", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// TODO: Add assertions for the response body or other expectations
})

t.Run("Test JSON Page - json2Yaml", func(t *testing.T) {
// Create a test request to the "/json" route with action=json2Yaml and json=...
req := httptest.NewRequest(http.MethodGet, "/json?action=json2Yaml&json=...", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// TODO: Add assertions for the response body or other expectations
})
}
func TestUUIDPage(t *testing.T) {
app := newApp(BaseTestPath)
uuidPage(app)
// Create a test request to the "/uuid" route
req := httptest.NewRequest(http.MethodGet, "/uuid", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// TODO: Add assertions for the response body or other expectations
}

func TestULIDPage(t *testing.T) {
app := newApp(BaseTestPath)
ulidPage(app)
// Create a test request to the "/ulid" route
req := httptest.NewRequest(http.MethodGet, "/ulid", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// TODO: Add assertions for the response body or other expectations
}

func TestPasswordPage(t *testing.T) {
app := newApp(BaseTestPath)
passwordPage(app)
t.Run("Test Password Page", func(t *testing.T) {
// Create a test request to the "/password" route
req := httptest.NewRequest(http.MethodGet, "/password", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// TODO: Add assertions for the response body or other expectations
})
}
func TestYAMLPage(t *testing.T) {
app := newApp(BaseTestPath)
yamlPage(app)
t.Run("Test YAML Page - Beautify", func(t *testing.T) {
// Create a test request to the "/yaml" route with action=beautify and yaml=...
req := httptest.NewRequest(http.MethodGet, "/yaml?action=beautify&yaml=...", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// TODO: Add assertions for the response body or other expectations
})

t.Run("Test YAML Page - Minify", func(t *testing.T) {
// Create a test request to the "/yaml" route with action=minify and yaml=...
req := httptest.NewRequest(http.MethodGet, "/yaml?action=minify&yaml=...", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// TODO: Add assertions for the response body or other expectations
})

t.Run("Test YAML Page - yaml2JSON", func(t *testing.T) {
// Create a test request to the "/yaml" route with action=yaml2JSON and yaml=...
req := httptest.NewRequest(http.MethodGet, "/yaml?action=yaml2JSON&yaml=...", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// TODO: Add assertions for the response body or other expectations
})
}

func TestJWTPage(t *testing.T) {
app := newApp(BaseTestPath)
jwtPage(app)
t.Run("Test JWT Page", func(t *testing.T) {
// Create a test request to the "/jwt" route with jwt=...
req := httptest.NewRequest(http.MethodGet, "/jwt?jwt=...", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// TODO: Add assertions for the response body or other expectations
})
}

func TestBase64Page(t *testing.T) {
app := newApp(BaseTestPath)
base64Page(app)
t.Run("Test Base64 Page - Encode", func(t *testing.T) {
// Create a test request to the "/base64" route with action=encode and decoded=...
req := httptest.NewRequest(http.MethodGet, "/base64?action=encode&decoded=...", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// TODO: Add assertions for the response body or other expectations
})

t.Run("Test Base64 Page - Decode", func(t *testing.T) {
// Create a test request to the "/base64" route with action=decode and encoded=...
req := httptest.NewRequest(http.MethodGet, "/base64?action=decode&encoded=...", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// TODO: Add assertions for the response body or other expectations
})
}

func TestDefineResources(t *testing.T) {
app := newApp(BaseTestPath)
defineResources(app, "../ui")

t.Run("Test Static /css", func(t *testing.T) {
// Create a test request to the "/css" route
req := httptest.NewRequest(http.MethodGet, "/css/bootstrap.min.css", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// TODO: Add assertions for the response body or other expectations
})

t.Run("Test Static /js", func(t *testing.T) {
// Create a test request to the "/js" route
req := httptest.NewRequest(http.MethodGet, "/js/bootstrap.min.js", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// TODO: Add assertions for the response body or other expectations
})

t.Run("Test Static /img", func(t *testing.T) {
// Create a test request to the "/img" route
req := httptest.NewRequest(http.MethodGet, "/img/logo-transparent.png", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// TODO: Add assertions for the response body or other expectations
})
}

0 comments on commit a8af1b1

Please sign in to comment.