<commit_before>package tracercheck

import (
  _ "fmt"
  "time"
  "bytes"
  "testing"
  "net/http"
  _ "encoding/base64"
  "net/http/httptest"

  "github.com/gorilla/mux"

  "github.com/r4start/web-tracer/tracer"
)

func TestLogSingleMessage(t *testing.T) {
  dbName := ":memory:"
  logger, err := tracer.NewDbLogger(dbName)
  if err != nil {
    t.Fatal(err);
  }

  var idsCache = tracer.NewTerminalIdsCache()
  logger.IdsCache = &idsCache

  logger.PrepareDB()

  router := mux.NewRouter()
  router.Handle("/terminal/{id:[0-9]+}", logger)

  recorder := httptest.NewRecorder()
  url := "/terminal/177"
  jsonRequest := []byte(`{ "message" : "helo youg" }`)

  req, e := http.NewRequest("POST", url, bytes.NewBuffer(jsonRequest))
  if  e != nil {
    t.Fatal(e)
  }

  go router.ServeHTTP(recorder, req)

  // Timeout necessary, because the server must have some
  // extra time for addind a new record.
  time.Sleep(5000)

  req, e = http.NewRequest("GET", url, nil)
  if e != nil {
    t.Fatal(e)
  }

  recorder = httptest.NewRecorder()

  router.ServeHTTP(recorder, req)

  t.Log("Request and responce are not equal! ",
        string(jsonRequest),
        recorder.Body.String())
  
}
<commit_msg>Finished with first test case.<commit_after>package tracercheck

import (
  _ "fmt"
  "io"
  "time"
  "bytes"
  "testing"
  "net/http"
  "encoding/json"
  "encoding/base64"
  "net/http/httptest"

  "github.com/gorilla/mux"

  "github.com/r4start/web-tracer/tracer"
)

func TestLogSingleMessage(t *testing.T) {
  dbName := ":memory:"
  logger, err := tracer.NewDbLogger(dbName)
  if err != nil {
    t.Fatal(err);
  }

  var idsCache = tracer.NewTerminalIdsCache()
  logger.IdsCache = &idsCache

  logger.PrepareDB()

  router := mux.NewRouter()
  router.Handle("/terminal/{id:[0-9]+}", logger)

  recorder := httptest.NewRecorder()
  url := "/terminal/177"
  messageText := "helo youg"
  jsonRequest := []byte(`{ "message" : "` + messageText + `" }`)

  req, e := http.NewRequest("POST", url, bytes.NewBuffer(jsonRequest))
  if  e != nil {
    t.Fatal(e)
  }

  go router.ServeHTTP(recorder, req)

  // Timeout is necessary due the server must have some
  // extra time for adding a new record.
  time.Sleep(5000)

  req, e = http.NewRequest("GET", url, nil)
  if e != nil {
    t.Fatal(e)
  }

  recorder = httptest.NewRecorder()

  router.ServeHTTP(recorder, req)

  t.Log("Got responce ", recorder.Body.String())
  
  type entryType struct {
    Timestamp string `json:"timestamp"`
    Message string `json:"message"`
  }
  type responseType struct {
    Entries []entryType `json:"entries"`
  }

  jsonDecoder := json.NewDecoder(recorder.Body)

  var logEntries responseType
  err = jsonDecoder.Decode(&logEntries)
  if err != nil && err != io.EOF {
    t.Fatal(err)
  }

  if len(logEntries.Entries) != 1 {
    t.Fatal("Log entries length doesn`t equal")
  }

  for i, v := range logEntries.Entries {
    decodeBytes, err := base64.StdEncoding.DecodeString(v.Message)
    if err != nil {
      t.Fatal(err)
    }

    logEntries.Entries[i].Message = string(decodeBytes)
  }

  t.Log("Decoded json ", logEntries)

  if logEntries.Entries[0].Message != messageText {
    t.Fatal("Message text is wrong!\n Got ", logEntries.Entries[0].Message,
            "\nShould be ", messageText)
  }
}

{'fraction_non_alphanumeric': 0.15195632393084624, 'fraction_numerical': 0.008795875037913254, 'mean_word_length': 3.255483870967742, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4642302', 'n_tokens_mistral': 1240, 'n_tokens_neox': 1163, 'n_words': 253}