Repository: Kwynto/gosession
Branch: main
Commit: 7822ad47b237
Files: 26
Total size: 61.9 KB
Directory structure:
gitextract_1luo0k9u/
├── .github/
│ └── workflows/
│ └── go.yaml
├── .gitignore
├── LICENSE
├── README.md
├── go.mod
├── gosession.go
├── gosession_test.go
└── pkg/
└── examples/
├── example1/
│ ├── go.mod
│ ├── go.sum
│ └── main.go
├── example2/
│ ├── go.mod
│ ├── go.sum
│ └── main.go
└── example3/
├── cmd/
│ └── web/
│ ├── handlers.go
│ ├── helpers.go
│ ├── main.go
│ ├── routes.go
│ └── templates.go
├── go.mod
├── go.sum
└── ui/
├── html/
│ ├── base.layout.tmpl
│ ├── footer.partial.tmpl
│ ├── home.page.tmpl
│ └── homeauth.page.tmpl
└── static/
├── css/
│ └── main.css
└── js/
└── main.js
================================================
FILE CONTENTS
================================================
================================================
FILE: .github/workflows/go.yaml
================================================
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
name: GoCoverage # The name of the workflow that will appear on Github
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: Build
run: go build -v ./...
- name: Test
run: go test -coverprofile="coverage.txt" -v ./...
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
================================================
FILE: .gitignore
================================================
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool
*.out
PR
================================================
FILE: LICENSE
================================================
MIT License
Copyright (c) 2022 Constantine Zavezeon
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
# GoSession
This is quick session for net/http in GoLang.
This package is perhaps the best implementation of the session mechanism, at least it tries to become one.
[](https://github.com/avelino/awesome-go)
[](https://godoc.org/github.com/Kwynto/gosession)
[](https://goreportcard.com/report/github.com/Kwynto/gosession)
[](https://github.com/Kwynto/gosession/blob/master/LICENSE)
[](https://codecov.io/gh/Kwynto/gosession)
**Important note**
This package is designed to work with the standard net/http package and has not been tested with other http packages by the developer.
## Contents
- [GoSession](#gosession)
- [Contents](#contents)
- [What are sessions and why are they needed](#what-are-sessions-and-why-are-they-needed)
- [How to connect GoSession](#how-to-connect-gosession)
- [How to use GoSession](#how-to-use-gosession)
- [Examples of using](#examples-of-using)
- [Example 1:](#example-1)
- [Example 2:](#example-2)
- [Example 3:](#example-3)
- [About the package](#about-the-package)
- [About the author](#about-the-author)
## What are sessions and why are they needed
A session on a site is a good method of identifying a site user.
A session is often used to authorize a user and retain their identity until the user closes the browser page.
While the user is working with the site, he saves cookies with a unique identifier, by this identifier one can distinguish one user from another and the server can store special data for a particular user.
User data received during the session period can be used for authorization, marketing and many other cases when it is necessary to collect, process and analyze data about a specific user.
A session is an efficient method of interacting with a user.
**[⬆ back to top](#gosession)** - **[⬆ back to the chapter](#what-are-sessions-and-why-are-they-needed)**
## How to connect GoSession
In your project folder, initialize the Go-module with the command
> go mod init your_app_name
Download and install GoSession
> go get github.com/Kwynto/gosession
Now you can add the GoSession package to your Go-code file, for example in `main.go`
```go
import "github.com/Kwynto/gosession"
```
**[⬆ back to top](#gosession)** - **[⬆ back to the chapter](#how-to-connect-gosession)**
## How to use GoSession
To use the GoSession package, you need to import it into your code.
```go
import "github.com/Kwynto/gosession"
```
All operations for working with sessions must be called from handlers.
Each time you start working with the session store, you need to call `gosession.Start(w *http.ResponseWriter, r *http.Request)`, since this function returns the identifier of the store and allows you to access the elements of the store through the identifier.
```go
id := gosession.Start(&w, r)
```
You need to call the `gosession.Start(w *http.ResponseWriter, r *http.Request)` function from the handler
```go
func rootHandler(w http.ResponseWriter, r *http.Request) {
id := gosession.Start(&w, r) // Get the storage ID for a specific user
html := "Title%s"
fmt.Fprintf(w, html, id)
}
```
Alternatively, you can use the `gosession.StartSecure(w *http.ResponseWriter, r *http.Request)` function instead of `gosession.Start(w, r)`.
The `StartSecure()` function replaces the session ID each time it is accessed, which reduces the possibility of ID hijacking.
The use of these functions is exactly the same.
```go
id := gosession.StartSecure(&w, r)
```
You need to call the `gosession.StartSecure()` function from the handler
```go
func rootHandler(w http.ResponseWriter, r *http.Request) {
id := gosession.StartSecure(&w, r) // Get the storage ID for a specific user
html := "Title%s"
fmt.Fprintf(w, html, id)
}
```
Once you have a store ID, you can write variables to the store, read them, and delete them.
Recording is done using the `Set(name string, value interface{})` method
```go
id.Set("name variable", anyVariable)
```
In the handler it looks like this
```go
func writeHandler(w http.ResponseWriter, r *http.Request) {
name := "username"
username := "JohnDow"
id := gosession.Start(&w, r)
id.Set(name, username)
html := "TitleOK"
fmt.Fprint(w, html)
}
```
Reading is done by `Get(name string) interface{}` method for one variable
and the `GetAll() Session` method to read all session variables
```go
anyVariable := id.Get("name variable")
```
```go
allVariables := id.GetAll()
```
In the handler it looks like this
```go
func readHandler(w http.ResponseWriter, r *http.Request) {
name := "username"
var username interface{}
id := gosession.Start(&w, r)
username = id.Get(name) // Reading the "username" variable from the session for a specific user
html := "Title%s"
fmt.Fprintf(w, html, username)
}
```
or so
```go
func readHandler(w http.ResponseWriter, r *http.Request) {
var tempStr string = ""
id := gosession.Start(&w, r)
allVariables := id.GetAll() // Reading the entire session for a specific client
for i, v := range allVariables {
tempStr = fmt.Sprint(tempStr, i, "=", v, " ")
}
html := "Title%s"
fmt.Fprintf(w, html, tempStr)
}
```
Removing an entry from a session of a specific client is carried out using the `Remove(name string)` method
```go
id.Remove("name variable")
```
In the handler it looks like this
```go
func removeHandler(w http.ResponseWriter, r *http.Request) {
id := gosession.Start(&w, r)
id.Remove("name variable") // Removing a variable from a specific client session
html := "TitleOK"
fmt.Fprint(w, html)
}
```
Removing the entire session of a specific client is done using the `Destroy(w *http.ResponseWriter)` method
```go
id.Destroy(&w)
```
In the handler it looks like this
```go
func destroyHandler(w http.ResponseWriter, r *http.Request) {
id := gosession.Start(&w, r)
id.Destroy(&w) // Deleting the entire session of a specific client
html := "TitleOK"
fmt.Fprint(w, html)
}
```
GoSession allows you to change its settings with the `SetSettings(setings GoSessionSetings)` function,
which is used outside of the handler, for example, inside the `main()` function
```go
var mySetingsSession = gosession.GoSessionSetings{
CookieName: gosession.GOSESSION_COOKIE_NAME,
Expiration: gosession.GOSESSION_EXPIRATION,
TimerCleaning: gosession.GOSESSION_TIMER_FOR_CLEANING,
}
gosession.SetSetings(mySetingsSession) // Setting session preferences
```
GoSession has 3 constants available for use
```go
const (
GOSESSION_COOKIE_NAME string = "SessionId" // Name for session cookies
GOSESSION_EXPIRATION int64 = 43_200 // Max age is 12 hours.
GOSESSION_TIMER_FOR_CLEANING time.Duration = time.Hour // The period of launch of the mechanism of cleaning from obsolete sessions
)
```
The remaining functions, types and variables in GoSession are auxiliary and are used only within the package.
**[⬆ back to top](#gosession)** - **[⬆ back to the chapter](#how-to-use-gosession)**
## Examples of using
### Example 1:
*This is a simple authorization example and it shows the use of the write and read session variables functions, as well as deleting the entire session.*
Download the GoSession project to your computer:
> git clone https://github.com/Kwynto/gosession.git
Go to the example folder or open this folder in your IDE.
> cd ./gosession/pkg/example1
Install GoSession
> go get github.com/Kwynto/gosession
Run:
> go mod tidy
Start the server:
> go run .
Visit site
> http://localhost:8080/
### Example 2:
*This example shows a primitive way to collect information about user actions. You can collect any public user data, as well as track user actions, and then save and process this data.*
Download the GoSession project to your computer:
> git clone https://github.com/Kwynto/gosession.git
Go to the example folder or open this folder in your IDE.
> cd ./gosession/pkg/example2
Install GoSession
> go get github.com/Kwynto/gosession
Run:
> go mod tidy
Start the server:
> go run .
Visit site
> http://localhost:8080/
Now you can follow the links on this site and see how the site saves and shows your browsing history.
### Example 3:
*This example shows a simple, realistic site that uses the session mechanism.*
Download the GoSession project to your computer:
> git clone https://github.com/Kwynto/gosession.git
Go to the example folder or open this folder in your IDE.
> cd ./gosession/pkg/example3
Install GoSession
> go get github.com/Kwynto/gosession
Run:
> go mod tidy
Start the server:
> go run ./cmd/web/
Visit site
> http://localhost:8080/
Now you can follow the links on this site.
**[⬆ back to top](#gosession)** - **[⬆ back to the chapter](#examples-of-using)**
## About the package
GoSession has a description of its functionality in a `README.md` file and internal documentation.
GoSession is tested and has a performance check.
You can use the GoSession tests and documentation yourself.
Download the GoSession project to your computer:
> git clone https://github.com/Kwynto/gosession.git
Go to the project folder:
> cd ./gosession
**Check out the documentation**
Look at the documentation in two steps.
First, in the console, run:
> godoc -http=:8080
And then in your web browser navigate to the uri:
> http://localhost:8080
*The `godoc` utility may not be present in your Go build and you may need to install it
command `go get -v golang.org/x/tools/cmd/godoc`*
**For Debian Linux users (Ubuntu, Mint and others):** *You may need to install the tools with the `sudo apt install golang-golang-x-tools` command*
You can also use Go's standard functionality to view documentation in the console via `go doc`.
For example:
> go doc Start
If your IDE is good enough, then the documentation for functions and methods will be available from your code editor.
**Testing**
Run tests:
> go test -v
Run tests showing code coverage:
> go test -cover -v
You can view code coverage in detail in your web browser.
To do this, you need to sequentially execute two commands in the console:
> go test -coverprofile="coverage.out" -v
> go tool cover -html="coverage.out"
**Performance**
You can look at code performance tests:
> go test -benchmem -bench="." gosession.go gosession_test.go
*The slowest of all functions is `cleaningSessions()`, but this should not scare you, as it is a utility function and is rarely executed. This function does not affect the performance of the entire mechanism, it is only needed to clean up the storage from lost sessions.*
**[⬆ back to top](#gosession)** - **[⬆ back to the chapter](#about-the-package)**
## Support the author
You can support open source projects and the author of this project. The details are [here](https://github.com/Kwynto/Kwynto/blob/main/SUPPORT.md).
## About the author
The author of the project is Constantine Zavezeon (Kwynto).
You can contact the author by e-mail: kwynto@mail.ru
The author accepts proposals for participation in open source projects,
as well as willing to accept job offers.
If you want to offer me a job, then first I ask you to read [this](https://github.com/Kwynto/Kwynto/blob/main/offer.md).
**[⬆ back to top](#gosession)**
================================================
FILE: go.mod
================================================
module github.com/Kwynto/gosession
go 1.17
================================================
FILE: gosession.go
================================================
// This is quick session for net/http in golang.
// This package is perhaps the best implementation of the session mechanism, at least it tries to become one.
package gosession
// --------------------------------------------------------
// Copyright (c) 2022 Constantine Zavezeon
// --------------------------------------------------------
import (
"crypto/rand"
"fmt"
"net/http"
"sync"
"time"
)
const (
GOSESSION_COOKIE_NAME string = "SessionId" // Name for session cookies
GOSESSION_EXPIRATION int64 = 43_200 // Max age is 12 hours.
GOSESSION_TIMER_FOR_CLEANING time.Duration = time.Hour // The period of launch of the mechanism of cleaning from obsolete sessions
)
// The SessionId type is the session identifier
type SessionId string
// The Session type contains variables defined for session storage for each client.
type Session map[string]interface{}
// The internalSession type is the internal server representation of the session
type internalSession struct {
expiration int64
data Session
}
// The serverSessions type is intended to describe all sessions of all client connections
type serverSessions map[SessionId]internalSession
// The GoSessionSetings type describes the settings for the session system
type GoSessionSetings struct {
CookieName string
Expiration int64
TimerCleaning time.Duration
}
// The allSessions variable stores all sessions of all clients
var allSessions serverSessions = make(serverSessions, 0)
// Session mechanism settings variable
var setingsSession = GoSessionSetings{
CookieName: GOSESSION_COOKIE_NAME,
Expiration: GOSESSION_EXPIRATION,
TimerCleaning: GOSESSION_TIMER_FOR_CLEANING,
}
var block sync.RWMutex
// The generateId() generates a new session id in a random, cryptographically secure manner
func generateId() SessionId {
b := make([]byte, 32)
rand.Read(b)
return SessionId(fmt.Sprintf("%x", b))
}
// The getOrSetCookie(w, r) gets the session id from the cookie, or creates a new one if it can't get
func getOrSetCookie(w *http.ResponseWriter, r *http.Request) SessionId {
data, err := r.Cookie(setingsSession.CookieName)
if err != nil {
id := generateId()
cookie := &http.Cookie{
Name: setingsSession.CookieName,
Value: string(id),
MaxAge: 0,
}
http.SetCookie(*w, cookie)
return id
}
return SessionId(data.Value)
}
// The deleteCookie(w) function deletes the session cookie
func deleteCookie(w *http.ResponseWriter) {
cookie := &http.Cookie{
Name: setingsSession.CookieName,
Value: "",
MaxAge: -1,
}
http.SetCookie(*w, cookie)
}
// The cleaningSessions() function periodically cleans up the server's session storage
func cleaningSessions() {
presently := time.Now().Unix()
block.Lock()
for id, ses := range allSessions {
if ses.expiration < presently {
delete(allSessions, id)
}
}
block.Unlock()
// log.Println("Session storage has been serviced.")
time.AfterFunc(setingsSession.TimerCleaning, cleaningSessions)
}
// The writeS() method safely writes data to the session store
func (id SessionId) writeS(iSes internalSession) {
block.Lock()
allSessions[id] = iSes
block.Unlock()
}
// The readS() method safely reads data from the session store.
func (id SessionId) readS() (internalSession, bool) {
block.RLock()
defer block.RUnlock()
ses, ok := allSessions[id]
if !ok {
return internalSession{}, false
}
return ses, true
}
// The destroyS() method safely deletes the entire session from the store.
func (id SessionId) destroyS() {
block.Lock()
delete(allSessions, id)
block.Unlock()
}
// The deleteS() method safely deletes one client variable from the session by its name
// name - session variable name
func (id SessionId) deleteS(name string) {
block.Lock()
ses, ok := allSessions[id]
if ok {
delete(ses.data, name)
allSessions[id] = ses
}
block.Unlock()
}
// The Set(name, value) SessionId-method to set the client variable to be stored in the session system.
// name - session variable name.
// value - directly variable in session.
func (id SessionId) Set(name string, value interface{}) {
ses, ok := id.readS()
if ok {
ses.data[name] = value
id.writeS(ses)
}
}
// The GetAll() SessionId-method to get all client variables from the session system
func (id SessionId) GetAll() Session {
ses, _ := id.readS()
return ses.data
}
// The Get(name) SessionId-method to get a specific client variable from the session system.
// name - session variable name
func (id SessionId) Get(name string) interface{} {
ses, _ := id.readS()
return ses.data[name]
}
// The Destroy(w) SessionId-method to remove the entire client session
func (id SessionId) Destroy(w *http.ResponseWriter) {
id.destroyS()
deleteCookie(w)
}
// The Remove(name) SessionId-method to remove one client variable from the session by its name
func (id SessionId) Remove(name string) {
id.deleteS(name)
}
// The SetSetings(settings) sets new settings for the session mechanism.
// setings - gosession.GoSessionSetings public type variable for setting new session settings
func SetSetings(setings GoSessionSetings) {
setingsSession = setings
}
// The Start(w, r) function starts the session and returns the SessionId to the handler for further use of the session mechanism.
// This function must be run at the very beginning of the http.Handler
func Start(w *http.ResponseWriter, r *http.Request) SessionId {
id := getOrSetCookie(w, r)
ses, ok := id.readS()
if !ok {
ses.data = make(Session, 0)
}
presently := time.Now().Unix()
ses.expiration = presently + setingsSession.Expiration
id.writeS(ses)
return id
}
// The StartSecure(w, r) function starts the session or changes the session ID and sets new cookie to the client.
// This function must be run at the very beginning of the http.Handler
func StartSecure(w *http.ResponseWriter, r *http.Request) SessionId {
id := getOrSetCookie(w, r)
ses, ok := id.readS()
if !ok {
ses.data = make(Session, 0)
presently := time.Now().Unix()
ses.expiration = presently + setingsSession.Expiration
id.writeS(ses)
return id
} else {
id.destroyS()
id = generateId()
cookie := &http.Cookie{
Name: setingsSession.CookieName,
Value: string(id),
MaxAge: 0,
}
http.SetCookie(*w, cookie)
presently := time.Now().Unix()
ses.expiration = presently + setingsSession.Expiration
id.writeS(ses)
return id
}
}
// Package initialization
func init() {
time.AfterFunc(setingsSession.TimerCleaning, cleaningSessions)
// log.Println("GoSessions initialized")
}
================================================
FILE: gosession_test.go
================================================
package gosession
// --------------------------------------------------------
// Copyright (c) 2022 Constantine Zavezeon
// --------------------------------------------------------
import (
"fmt"
"io"
"math/rand"
"net/http"
"net/http/httptest"
"testing"
"time"
)
const (
GOSESSION_TESTING_ITER int = 100
)
// --------------
// Test functions
// --------------
func Test_generateId(t *testing.T) {
testVar := make(map[int]SessionId)
for i := 0; i < GOSESSION_TESTING_ITER; i++ {
testVar[i] = generateId() // calling the tested function
}
for _, v1 := range testVar {
count := 0
for _, v2 := range testVar {
if v1 == v2 {
count++
}
// if bytes.Equal([]byte(v1), []byte(v2)) {
// count++
// }
}
// work check
if count > 1 {
t.Error("Error generating unique identifier.")
}
}
}
func Test_getOrSetCookie(t *testing.T) {
for i := 0; i < GOSESSION_TESTING_ITER; i++ {
var ctrlId SessionId
handler := func(w http.ResponseWriter, r *http.Request) {
sesid := getOrSetCookie(&w, r) // calling the tested function
ctrlId = sesid
io.WriteString(w, string(sesid))
}
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
handler(w, r)
status := w.Code
// work check
if status != http.StatusOK {
t.Errorf("Handler returned %v", status)
}
cookies := w.Result().Cookies()
noErr := false
for _, v := range cookies {
if v.Name == setingsSession.CookieName && v.Value == string(ctrlId) {
noErr = true
}
}
// work check
if !noErr {
t.Error("the server returned an invalid ID")
}
}
for i := 0; i < GOSESSION_TESTING_ITER; i++ {
var ctrlId SessionId
handler := func(w http.ResponseWriter, r *http.Request) {
sesid := getOrSetCookie(&w, r) // calling the tested function
ctrlId = sesid
io.WriteString(w, string(sesid))
}
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
clientId := generateId()
cookie := &http.Cookie{
Name: setingsSession.CookieName,
Value: string(clientId),
MaxAge: 0,
}
r.AddCookie(cookie)
handler(w, r)
status := w.Code
// work check
if status != http.StatusOK {
t.Errorf("Handler returned %v", status)
}
// work check
if ctrlId != clientId {
t.Error("server received invalid id")
}
}
}
func Test_deleteCookie(t *testing.T) {
for i := 0; i < GOSESSION_TESTING_ITER; i++ {
// handler := func(w http.ResponseWriter, r *http.Request) {
handler := func(w http.ResponseWriter) {
deleteCookie(&w) // calling the tested function
io.WriteString(w, "TitleBody")
}
w := httptest.NewRecorder()
// r := httptest.NewRequest("GET", "/", nil)
clientId := generateId()
// cookie := &http.Cookie{
// Name: setingsSession.CookieName,
// Value: string(clientId),
// MaxAge: 0,
// }
// r.AddCookie(cookie)
handler(w)
status := w.Code
// work check
if status != http.StatusOK {
t.Errorf("Handler returned %v", status)
}
cookies := w.Result().Cookies()
noErr := true
for _, v := range cookies {
if v.Name == setingsSession.CookieName && v.Value == string(clientId) {
noErr = false
}
}
// work check
if !noErr {
t.Error("the server did not delete the session cookie")
}
}
}
func Test_cleaningSessions(t *testing.T) {
rand.Seed(time.Now().Unix())
var falseInd int
var trueInd int
for i := 0; i < GOSESSION_TESTING_ITER; i++ {
falseInd = rand.Intn(75)
trueInd = rand.Intn(50) + falseInd
for id := range allSessions {
delete(allSessions, id)
}
for fi := 0; fi < falseInd; fi++ {
allSessions[generateId()] = internalSession{
expiration: 0,
data: make(Session),
}
}
for ti := 0; ti < trueInd; ti++ {
allSessions[generateId()] = internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: make(Session),
}
}
cleaningSessions() // calling the tested function
// work check
if len(allSessions) != trueInd {
t.Error("The number of correct entries does not match.")
}
}
}
func Test_writeS(t *testing.T) {
for i := 0; i < GOSESSION_TESTING_ITER; i++ {
ses := internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: make(Session),
}
id := generateId()
id.writeS(ses)
if allSessions[id].expiration != ses.expiration {
t.Error("Writing error. Session is not equal.")
}
}
}
func Test_readS(t *testing.T) {
for i := 0; i < GOSESSION_TESTING_ITER; i++ {
ses := internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: make(Session),
}
id := generateId()
allSessions[id] = ses
res, _ := id.readS()
if res.expiration != ses.expiration {
t.Error("Reading error. Session is not equal.")
}
delete(allSessions, id)
_, ok := id.readS()
if ok {
t.Error("Reading error. Was reaing wrong session.")
}
}
}
func Test_destroyS(t *testing.T) {
for i := 0; i < GOSESSION_TESTING_ITER; i++ {
ses := internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: make(Session),
}
id := generateId()
id.writeS(ses)
id.destroyS()
_, ok := id.readS()
if ok {
t.Error("Destroy error. Was reading deleted session.")
}
}
}
func Test_deleteS(t *testing.T) {
for i := 0; i < GOSESSION_TESTING_ITER; i++ {
ses := internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: make(Session),
}
ses.data["name"] = "test string"
id := generateId()
id.writeS(ses)
// id.destroyS()
id.deleteS("name")
_, ok := allSessions[id].data["name"]
if ok {
t.Error("Delete error. Was reading deleted variable.")
}
}
}
func Test_Set(t *testing.T) {
var value interface{}
rand.Seed(time.Now().Unix())
for i := 0; i < GOSESSION_TESTING_ITER; i++ {
id := generateId()
allSessions[id] = internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: make(Session),
}
name := "test variable"
switch rand.Intn(3) {
case 0:
value = true
case 1:
value = fmt.Sprintf("test string %d", rand.Intn(100))
case 2:
value = rand.Intn(100)
case 3:
value = rand.Float64()
}
id.Set(name, value) // calling the tested function
// work check
if allSessions[id].data[name] != value {
t.Error("Failed to write variable to session storage.")
}
}
}
func Test_GetAll(t *testing.T) {
var value interface{}
var name string
rand.Seed(time.Now().Unix())
for i := 0; i < GOSESSION_TESTING_ITER; i++ {
id := generateId()
data := make(Session)
count := rand.Intn(20) + 1
for ic := 0; ic < count; ic++ {
name = fmt.Sprintf("test name %d", rand.Intn(100))
switch rand.Intn(3) {
case 0:
value = true
case 1:
value = fmt.Sprintf("test string %d", rand.Intn(100))
case 2:
value = rand.Intn(100)
case 3:
value = rand.Float64()
}
data[name] = value
}
allSessions[id] = internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: data,
}
ses := id.GetAll() // calling the tested function
// work check
for iname, v := range ses {
if v != data[iname] {
t.Error("Incorrect data received from session variable storage")
}
}
}
}
func Test_Get(t *testing.T) {
var value interface{}
var name string
rand.Seed(time.Now().Unix())
for i := 0; i < GOSESSION_TESTING_ITER; i++ {
id := generateId()
data := make(Session)
name = "test name"
switch rand.Intn(4) {
case 0:
value = true
case 1:
value = fmt.Sprintf("test string %d", rand.Intn(100))
case 2:
value = rand.Intn(100)
case 3:
value = rand.Float64()
}
data[name] = value
allSessions[id] = internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: data,
}
getedValue := id.Get(name) // calling the tested function
// work check
if getedValue != value {
t.Error("Incorrect data received from session variable storage")
}
}
}
func Test_Destroy(t *testing.T) {
var hid SessionId
for i := 0; i < GOSESSION_TESTING_ITER; i++ {
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
id := generateId()
cookie := &http.Cookie{
Name: setingsSession.CookieName,
Value: string(id),
MaxAge: 0,
}
r.AddCookie(cookie)
data := make(Session)
name := "test name"
value := "test value"
data[name] = value
allSessions[id] = internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: data,
}
handler := func(w http.ResponseWriter, r *http.Request) {
hid = Start(&w, r)
hid.Destroy(&w) // calling the tested function
io.WriteString(w, "TitleBody")
}
handler(w, r)
status := w.Code
// work check
if status != http.StatusOK {
t.Errorf("Handler returned status: %v", status)
}
// work check
if id != hid {
t.Error("ID mismatch")
}
cookies := w.Result().Cookies()
noErr := true
for _, v := range cookies {
if v.Name == setingsSession.CookieName && v.Value == string(id) {
noErr = false
}
}
// work check
if !noErr {
t.Error("The server did not delete the session cookie")
}
// work check
if allSessions[id].data != nil {
t.Error("Session has not been deleted.")
}
}
}
func Test_Remove(t *testing.T) {
var value interface{}
var name string
rand.Seed(time.Now().Unix())
for i := 0; i < GOSESSION_TESTING_ITER; i++ {
id := generateId()
data := make(Session)
name = "test name"
switch rand.Intn(3) {
case 0:
value = true
case 1:
value = fmt.Sprintf("test string %d", rand.Intn(100))
case 2:
value = rand.Intn(100)
case 3:
value = rand.Float64()
}
data[name] = value
allSessions[id] = internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: data,
}
id.Remove(name) // calling the tested function
// work check
if allSessions[id].data[name] == value {
t.Error("Failed to change settings")
}
}
}
func Test_SetSetings(t *testing.T) {
var test_setingsSession1 = GoSessionSetings{
CookieName: "test_name",
Expiration: int64(rand.Intn(86_400)),
TimerCleaning: time.Minute,
}
var test_setingsSession2 = GoSessionSetings{
CookieName: GOSESSION_COOKIE_NAME,
Expiration: GOSESSION_EXPIRATION,
TimerCleaning: GOSESSION_TIMER_FOR_CLEANING,
}
for i := 0; i < GOSESSION_TESTING_ITER; i++ {
SetSetings(test_setingsSession1) // calling the tested function
// work check
if test_setingsSession1 != setingsSession {
t.Error("Failed to change settings.")
}
SetSetings(test_setingsSession2) // calling the tested function
// work check
if test_setingsSession2 != setingsSession {
t.Error("Failed to change settings.")
}
}
}
func Test_Start(t *testing.T) {
for i := 0; i < GOSESSION_TESTING_ITER; i++ {
var id1 SessionId
var id2 SessionId
handler1 := func(w http.ResponseWriter, r *http.Request) {
id1 = Start(&w, r) // calling the tested function
io.WriteString(w, "TitleBody")
}
handler2 := func(w http.ResponseWriter, r *http.Request) {
id2 = Start(&w, r) // calling the tested function
io.WriteString(w, "TitleBody")
}
w1 := httptest.NewRecorder()
r1 := httptest.NewRequest("GET", "/", nil)
handler1(w1, r1)
status := w1.Code
// work check
if status != http.StatusOK {
t.Errorf("Handler returned status: %v", status)
}
cookies := w1.Result().Cookies()
var cookie *http.Cookie
for _, v := range cookies {
cookie = v
}
w2 := httptest.NewRecorder()
r2 := httptest.NewRequest("GET", "/", nil)
r2.AddCookie(cookie)
handler2(w2, r2)
status = w2.Code
// work check
if status != http.StatusOK {
t.Errorf("Handler returned status: %v", status)
}
// work check
if id1 != id2 {
t.Errorf("Server and client IDs are not equal:\n server: %v\n client: %v\n", id1, id2)
}
}
}
func Test_StartSecure(t *testing.T) {
for i := 0; i < GOSESSION_TESTING_ITER; i++ {
var id1 SessionId
var id2 SessionId
handler1 := func(w http.ResponseWriter, r *http.Request) {
id1 = StartSecure(&w, r) // calling the tested function
io.WriteString(w, "TitleBody")
}
handler2 := func(w http.ResponseWriter, r *http.Request) {
id2 = StartSecure(&w, r) // calling the tested function
io.WriteString(w, "TitleBody")
}
w1 := httptest.NewRecorder()
r1 := httptest.NewRequest("GET", "/", nil)
handler1(w1, r1)
status := w1.Code
// work check
if status != http.StatusOK {
t.Errorf("Handler returned status: %v", status)
}
cookies := w1.Result().Cookies()
var cookie *http.Cookie
for _, v := range cookies {
cookie = v
}
w2 := httptest.NewRecorder()
r2 := httptest.NewRequest("GET", "/", nil)
r2.AddCookie(cookie)
handler2(w2, r2)
status = w2.Code
// work check
if status != http.StatusOK {
t.Errorf("Handler returned status: %v", status)
}
// work check
if id1 == id2 {
t.Errorf("Server and client IDs are equal:\n server: %v\n client: %v\n", id1, id2)
}
}
}
// ----------------------
// Functions benchmarking
// ----------------------
func Benchmark_generateId(b *testing.B) {
for i := 0; i < b.N; i++ {
generateId() // calling the tested function
}
}
func Benchmark_getOrSetCookie(b *testing.B) {
handler := func(w http.ResponseWriter, r *http.Request) {
getOrSetCookie(&w, r) // calling the tested function
}
r := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
for i := 0; i < b.N; i++ {
handler(w, r)
}
}
func Benchmark_deleteCookie(b *testing.B) {
// handler := func(w http.ResponseWriter, r *http.Request) {
handler := func(w http.ResponseWriter) {
deleteCookie(&w) // calling the tested function
}
w := httptest.NewRecorder()
// r := httptest.NewRequest("GET", "/", nil)
// cookie := &http.Cookie{
// Name: setingsSession.CookieName,
// Value: string(generateId()),
// MaxAge: 0,
// }
// r.AddCookie(cookie)
for i := 0; i < b.N; i++ {
handler(w)
}
}
func Benchmark_cleaningSessions(b *testing.B) {
for i := 0; i < b.N; i++ {
cleaningSessions() // calling the tested function
}
}
func Benchmark_writeS(b *testing.B) {
ses := internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: make(Session),
}
ses.data["name"] = "test value"
id := generateId()
for i := 0; i < b.N; i++ {
id.writeS(ses) // calling the tested function
}
}
func Benchmark_readS(b *testing.B) {
ses := internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: make(Session),
}
ses.data["name"] = "test value"
id := generateId()
id.writeS(ses)
for i := 0; i < b.N; i++ {
id.readS() // calling the tested function
}
}
func Benchmark_destroyS(b *testing.B) {
ses := internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: make(Session),
}
ses.data["name"] = "test value"
id := generateId()
id.writeS(ses)
for i := 0; i < b.N; i++ {
id.destroyS() // calling the tested function
}
}
func Benchmark_deleteS(b *testing.B) {
ses := internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: make(Session),
}
ses.data["name"] = "test value"
id := generateId()
id.writeS(ses)
for i := 0; i < b.N; i++ {
id.deleteS("name") // calling the tested function
}
}
func Benchmark_Set(b *testing.B) {
rand.Seed(time.Now().Unix())
id := generateId()
data := make(Session)
allSessions[id] = internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: data,
}
name := fmt.Sprintf("BenchName%d", rand.Intn(100))
value := rand.Float64()
for i := 0; i < b.N; i++ {
id.Set(name, value) // calling the tested function
}
}
func Benchmark_GetAll(b *testing.B) {
rand.Seed(time.Now().Unix())
id := generateId()
data := make(Session)
name := fmt.Sprintf("BenchName%d", rand.Intn(100))
value := rand.Float64()
data[name] = value
allSessions[id] = internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: data,
}
for i := 0; i < b.N; i++ {
id.GetAll() // calling the tested function
}
}
func Benchmark_Get(b *testing.B) {
rand.Seed(time.Now().Unix())
id := generateId()
data := make(Session)
name := fmt.Sprintf("BenchName%d", rand.Intn(100))
value := rand.Float64()
data[name] = value
allSessions[id] = internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: data,
}
for i := 0; i < b.N; i++ {
id.Get(name) // calling the tested function
}
}
func Benchmark_Destroy(b *testing.B) {
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
id := generateId()
cookie := &http.Cookie{
Name: setingsSession.CookieName,
Value: string(id),
MaxAge: 0,
}
r.AddCookie(cookie)
data := make(Session)
name := fmt.Sprintf("BenchName%d", rand.Intn(100))
value := rand.Float64()
data[name] = value
allSessions[id] = internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: data,
}
// handler := func(w http.ResponseWriter, r *http.Request) {
handler := func(w http.ResponseWriter) {
id.Destroy(&w) // calling the tested function
}
for i := 0; i < b.N; i++ {
handler(w)
}
}
func Benchmark_Remove(b *testing.B) {
rand.Seed(time.Now().Unix())
id := generateId()
data := make(Session)
name := fmt.Sprintf("BenchName%d", rand.Intn(100))
value := rand.Float64()
data[name] = value
allSessions[id] = internalSession{
expiration: time.Now().Unix() + setingsSession.Expiration,
data: data,
}
for i := 0; i < b.N; i++ {
id.Remove(name) // calling the tested function
}
}
func Benchmark_SetSetings(b *testing.B) {
var test_setingsSession = GoSessionSetings{
CookieName: GOSESSION_COOKIE_NAME,
Expiration: GOSESSION_EXPIRATION,
TimerCleaning: GOSESSION_TIMER_FOR_CLEANING,
}
for i := 0; i < b.N; i++ {
SetSetings(test_setingsSession) // calling the tested function
}
}
func Benchmark_Start(b *testing.B) {
handler := func(w http.ResponseWriter, r *http.Request) {
Start(&w, r) // calling the tested function
}
r := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
for i := 0; i < b.N; i++ {
handler(w, r)
}
}
func Benchmark_StartSecure(b *testing.B) {
handler := func(w http.ResponseWriter, r *http.Request) {
StartSecure(&w, r) // calling the tested function
}
r := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
for i := 0; i < b.N; i++ {
handler(w, r)
}
}
================================================
FILE: pkg/examples/example1/go.mod
================================================
module example1
go 1.17
require github.com/Kwynto/gosession v0.2.4
================================================
FILE: pkg/examples/example1/go.sum
================================================
github.com/Kwynto/gosession v0.0.0-20220620140922-d2e7b2c35b8e h1:gNsAZ2trdqTriYD0H1YvhsiMzm8hjM3dd1zK64hEndM=
github.com/Kwynto/gosession v0.0.0-20220620140922-d2e7b2c35b8e/go.mod h1:t9ebCczcC/w08whDLwShIZ59finO9EyRkiQf5B31m90=
github.com/Kwynto/gosession v0.2.1 h1:d0Dz1IBcjQ44nkAWkOz7hYEYrCfkyIjOIMnBhVy/GiE=
github.com/Kwynto/gosession v0.2.1/go.mod h1:t9ebCczcC/w08whDLwShIZ59finO9EyRkiQf5B31m90=
github.com/Kwynto/gosession v0.2.3 h1:8Ag0nXX0cCxrbFOFdCdPI9u46YtEEG/JFx1iHF+PH6I=
github.com/Kwynto/gosession v0.2.3/go.mod h1:t9ebCczcC/w08whDLwShIZ59finO9EyRkiQf5B31m90=
github.com/Kwynto/gosession v0.2.4 h1:SdK/0o1o88El3ALz/kFfZlrCfPBC6b6Czha6S5tb6vc=
github.com/Kwynto/gosession v0.2.4/go.mod h1:t9ebCczcC/w08whDLwShIZ59finO9EyRkiQf5B31m90=
================================================
FILE: pkg/examples/example1/main.go
================================================
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"net/http"
"github.com/Kwynto/gosession"
)
func GetMd5(text string) string {
h := md5.New()
h.Write([]byte(text))
return hex.EncodeToString(h.Sum(nil))
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
var html string = ""
id := gosession.Start(&w, r)
username := id.Get("username")
if username != nil {
html = `
Title
`
html = fmt.Sprintf(html, username, pasHash)
fmt.Fprint(w, html)
}
func secondHandler(w http.ResponseWriter, r *http.Request) {
id := gosession.Start(&w, r)
html := `
Title