• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stackus / sessions / 14847484626

05 May 2025 10:24PM UTC coverage: 78.595% (-0.04%) from 78.634%
14847484626

push

github

stackus
skip saving unchanged sessions

- Use a simple hash of the session data and cookie options to determine if the session data has changed.

33 of 42 new or added lines in 2 files covered. (78.57%)

481 of 612 relevant lines covered (78.59%)

22.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.0
/hash.go
1
package sessions
2

3
import (
4
        "crypto/sha256"
5
        "encoding/json"
6
        "slices"
7
)
8

9
type sessionHash[T any] struct {
10
        Data    T
11
        Options CookieOptions
12
}
13

14
func (s *sessionHash[T]) Hash() ([]byte, error) {
69✔
15
        // compute hash of the session data
69✔
16
        hasher := sha256.New()
69✔
17

69✔
18
        data, err := json.Marshal(s)
69✔
19
        if err != nil {
69✔
NEW
20
                return nil, err
×
NEW
21
        }
×
22
        _, err = hasher.Write(data)
69✔
23
        if err != nil {
69✔
NEW
24
                return nil, err
×
NEW
25
        }
×
26

27
        return hasher.Sum(nil), nil
69✔
28
}
29

30
func (s *sessionHash[T]) Compare(otherHash []byte) (bool, error) {
24✔
31
        // compute hash of the session data
24✔
32
        hash, err := s.Hash()
24✔
33
        if err != nil {
24✔
NEW
34
                return false, err
×
NEW
35
        }
×
36

37
        // compare the hashes
38
        return slices.Equal(hash, otherHash), nil
24✔
39
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc