Add GitHub alike identicons

This commit is contained in:
2022-01-12 15:32:31 +01:00
parent dac6aa1d8b
commit c93c9a58af
5 changed files with 200 additions and 40 deletions

View File

@ -3,12 +3,13 @@ package main
import (
"bytes"
_ "embed"
"github.com/gorilla/mux"
"image/png"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/gorilla/mux"
)
//go:embed testdata/1a79a4d60de6718e8e5b326e338ae533_v1.png
@ -17,6 +18,9 @@ var v1 []byte
//go:embed testdata/1a79a4d60de6718e8e5b326e338ae533_v2.png
var v2 []byte
//go:embed testdata/1a79a4d60de6718e8e5b326e338ae533_gh.png
var gh []byte
func testRouter() *mux.Router {
router := mux.NewRouter()
router.HandleFunc("/avatar/{id}", RequestHandler)
@ -65,6 +69,20 @@ func TestCorrectResponseForV2ColorScheme(t *testing.T) {
}
}
func TestCorrectResponseForGHColorSchemeAndPattern(t *testing.T) {
req, err := http.NewRequest("GET", "/avatar/1a79a4d60de6718e8e5b326e338ae533?c=gh&d=github", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
testRouter().ServeHTTP(rr, req)
if !reflect.DeepEqual(rr.Body.Bytes(), gh) {
t.Errorf("returned image does not match expected image")
}
}
func TestIgnoreCase(t *testing.T) {
w1 := bytes.NewBuffer([]byte{})
png.Encode(w1, genIdIcon("example", 80, colorV1))
@ -89,3 +107,30 @@ func TestStringMatchesHash(t *testing.T) {
t.Errorf("resulting images do not match")
}
}
func TestHSLtoRGB(t *testing.T) {
red := hslToRgba(0, 100, 50)
if red.R != 255 || red.G != 0 || red.B != 0 {
t.Errorf("Color red not as required")
}
green := hslToRgba(120, 100, 50)
if green.R != 0 || green.G != 255 || green.B != 0 {
t.Errorf("Color green not as required")
}
blue := hslToRgba(240, 100, 50)
if blue.R != 0 || blue.G != 0 || blue.B != 255 {
t.Errorf("Color blue not as required")
}
}
func TestShouldCreateNibbles(t *testing.T) {
hash := [16]byte{}
hash[0] = 0x12
nibbles := nibbles(hash)
if nibbles[0] != 0x01 || nibbles[1] != 02 {
t.Errorf("Nibbles not extracted as expected")
}
}