certichain

Blockchain built from scratch for verifying document's legitimacy.
Log | Files | Refs | README | LICENSE

commit 2d438931d004a6c4f3faa4a01280ad9bd40fcdef
parent c223e4562ceff250e8042aeb57adb7f7aff4ba37
Author: Shivakumar <[email protected]>
Date:   Mon, 10 Aug 2026 21:28:24 +0530

test(api): add tests for middleware

Signed-off-by: Shivakumar <[email protected]>

Diffstat:
Aapi/middleware_test.go | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 71 insertions(+), 0 deletions(-)

diff --git a/api/middleware_test.go b/api/middleware_test.go @@ -0,0 +1,71 @@ +// Copyright 2026 Shivakumar (Certichain) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package api + +import ( + "io" + "log" + "net/http" + "net/http/httptest" + "os" + "testing" +) + +func TestRecover(t *testing.T) { + t.Parallel() + + panicking := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + panic("boom") + }) + + log.SetOutput(io.Discard) + defer log.SetOutput(os.Stderr) + + handler := Recover(panicking) + + req := httptest.NewRequest(http.MethodGet, "/", nil) + rec := httptest.NewRecorder() + + handler.ServeHTTP(rec, req) + + if rec.Code != http.StatusInternalServerError { + t.Errorf("status mismatch: want = %d, got = %d", http.StatusInternalServerError, rec.Code) + } +} + +func TestRateLimit(t *testing.T) { + t.Parallel() + + ok := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + handler := RateLimit(1, 1)(ok) + + req := httptest.NewRequest(http.MethodGet, "/", nil) + + rec1 := httptest.NewRecorder() + handler.ServeHTTP(rec1, req) + if rec1.Code != http.StatusOK { + t.Errorf("status mismatch: want = %d, got = %d", http.StatusOK, rec1.Code) + } + + rec2 := httptest.NewRecorder() + handler.ServeHTTP(rec2, req) + if rec2.Code != http.StatusTooManyRequests { + t.Errorf("status mismatch: want = %d, got = %d", http.StatusTooManyRequests, rec2.Code) + } + +}