certichain

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

middleware_test.go (1817B)


      1 // Copyright 2026 Shivakumar (Certichain)
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package api
     16 
     17 import (
     18 	"io"
     19 	"log"
     20 	"net/http"
     21 	"net/http/httptest"
     22 	"os"
     23 	"testing"
     24 )
     25 
     26 func TestRecover(t *testing.T) {
     27 	t.Parallel()
     28 
     29 	panicking := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
     30 		panic("boom")
     31 	})
     32 
     33 	log.SetOutput(io.Discard)
     34 	defer log.SetOutput(os.Stderr)
     35 
     36 	handler := Recover(panicking)
     37 
     38 	req := httptest.NewRequest(http.MethodGet, "/", nil)
     39 	rec := httptest.NewRecorder()
     40 
     41 	handler.ServeHTTP(rec, req)
     42 
     43 	if rec.Code != http.StatusInternalServerError {
     44 		t.Errorf("status mismatch: want = %d, got = %d", http.StatusInternalServerError, rec.Code)
     45 	}
     46 }
     47 
     48 func TestRateLimit(t *testing.T) {
     49 	t.Parallel()
     50 
     51 	ok := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
     52 		w.WriteHeader(http.StatusOK)
     53 	})
     54 
     55 	handler := RateLimit(1, 1)(ok)
     56 
     57 	req := httptest.NewRequest(http.MethodGet, "/", nil)
     58 
     59 	rec1 := httptest.NewRecorder()
     60 	handler.ServeHTTP(rec1, req)
     61 	if rec1.Code != http.StatusOK {
     62 		t.Errorf("status mismatch: want = %d, got = %d", http.StatusOK, rec1.Code)
     63 	}
     64 
     65 	rec2 := httptest.NewRecorder()
     66 	handler.ServeHTTP(rec2, req)
     67 	if rec2.Code != http.StatusTooManyRequests {
     68 		t.Errorf("status mismatch: want = %d, got = %d", http.StatusTooManyRequests, rec2.Code)
     69 	}
     70 
     71 }