From 35194101b2b35df6d601c680859cd58beb5fdeea Mon Sep 17 00:00:00 2001 From: German Montalvo Yebenes Date: Tue, 5 Dec 2023 22:42:41 +0100 Subject: [PATCH] Test app initial commit --- .github/workflows/Build.yml | 22 ++++++++++----------- .github/workflows/Compliance.yml | 17 ---------------- .github/workflows/Meta.yml | 17 ---------------- app_test.go | 34 ++++++++++++++++++++++++++++++++ sampleGoApp.go | 16 +++++++++++++++ 5 files changed, 60 insertions(+), 46 deletions(-) delete mode 100644 .github/workflows/Compliance.yml delete mode 100644 .github/workflows/Meta.yml create mode 100644 app_test.go create mode 100644 sampleGoApp.go diff --git a/.github/workflows/Build.yml b/.github/workflows/Build.yml index dd0a48c..76453cc 100644 --- a/.github/workflows/Build.yml +++ b/.github/workflows/Build.yml @@ -1,20 +1,18 @@ -name: Building the application +name: Build and Test on: push: branches: - main jobs: - Build: + build: runs-on: ubuntu-latest steps: - - name: Checkout repository - uses: actions/checkout@v4 - with: - fetch-depth: 0 + - name: Set up Go + uses: actions/setup-go@v4 - # - name: Build - # uses: docker/build-push-action@v4 - # with: - # context: . - # push: true - # tags: user/app:latest \ No newline at end of file + - name: Build + run: | + go build -v ./... + + - name: Test + run: go test diff --git a/.github/workflows/Compliance.yml b/.github/workflows/Compliance.yml deleted file mode 100644 index 432189f..0000000 --- a/.github/workflows/Compliance.yml +++ /dev/null @@ -1,17 +0,0 @@ -name: Scan for vulnerabilities -on: - push: - branches: - - main -jobs: - Compliance: - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Container Scan - uses: crazy-max/ghaction-container-scan@v3.0.0 - with: - image: vulnerables/web-dvwa:latest - \ No newline at end of file diff --git a/.github/workflows/Meta.yml b/.github/workflows/Meta.yml deleted file mode 100644 index d3be444..0000000 --- a/.github/workflows/Meta.yml +++ /dev/null @@ -1,17 +0,0 @@ -name: Meta phase -on: - push: - branches: - - main -jobs: - Meta: - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Get changed files - run: | - git diff --name-only ${{ github.event.before }} ${{ github.sha }} \ No newline at end of file diff --git a/app_test.go b/app_test.go new file mode 100644 index 0000000..f18effe --- /dev/null +++ b/app_test.go @@ -0,0 +1,34 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +func TestHandler(t *testing.T) { + // Crear un request para pasar al handler. + req, err := http.NewRequest("GET", "/", nil) + if err != nil { + t.Fatal(err) + } + + // Crear un ResponseRecorder (httptest.ResponseWriter) para grabar las respuestas. + rr := httptest.NewRecorder() + handler := http.HandlerFunc(handler) + + // El handler satisface http.Handler, así que podemos llamar a su método ServeHTTP directamente + // y pasarle nuestro Request y ResponseRecorder. + handler.ServeHTTP(rr, req) + + // Comprobar el código de estado de la respuesta. + if status := rr.Code; status != http.StatusOK { + t.Errorf("handler devolvió un código incorrecto: obtuvo %v, esperaba %v", status, http.StatusOK) + } + + // Comprobar el cuerpo de la respuesta. + expected := `Hola, este es un servidor web básico en Go!` + if rr.Body.String() != expected { + t.Errorf("handler devolvió un cuerpo inesperado: obtuvo %v, esperaba %v", rr.Body.String(), expected) + } +} diff --git a/sampleGoApp.go b/sampleGoApp.go new file mode 100644 index 0000000..12fcfc4 --- /dev/null +++ b/sampleGoApp.go @@ -0,0 +1,16 @@ +package main + +import ( + "fmt" + "log" + "net/http" +) + +func handler(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hola, este es un servidor web básico en Go!") +} + +func main() { + http.HandleFunc("/", handler) + log.Fatal(http.ListenAndServe(":8080", nil)) +}