<commit_before>
//
// specifying shervin
//
// Sun Sep 21 15:35:29 JST 2014
//

#include "shervin.h"
#include "shv_protected.h" // direct access to fshv_request methods

#include "servman.h"
#include "flutil.h"
#include "bocla.h"


context "live"
{
  before all
  {
    server_start();
  }
  after all
  {
    server_stop();
    //system("lsof -i :4001");
  }

  describe "shervin"
  {
    before each
    {
      fcla_response *res = NULL;
    }
    after each
    {
      fcla_response_free(res);
    }

    it "serves /hello/toto"
    {
      res = fcla_get("http://127.0.0.1:4001/hello/toto");

      expect(res->status_code i== 200);
      expect(res->body === "hello toto\n");
    }

    it "serves back the body it received in /mirror"
    {
      res = fcla_post("http://127.0.0.1:4001/mirror", NULL, "nada");

      char *body = strstr(res->body, "\r\n\r\n") + 4;

      expect(res->status_code i== 200);
      expect(body === "nada");
    }

    it "serves files"
    {
      res = fcla_get("http://127.0.0.1:4001/files/a/b/hello.txt");

      //printf("res: %i\n", res->status_code);
      //if (res->headers)
      //{
      //  for (flu_node *n = res->headers->first; n; n = n->next)
      //  {
      //    printf("* %s: %s\n", n->key, (char *)n->item);
      //  }
      //}

      expect(res->status_code i== 200);
      expect(res->body === "hello world\n");

      expect(flu_list_get(res->headers, "x-accel-redirect") === ""
        "../spec/www/a/b/hello.txt");
    }

    it "doesn't actually serve the file if x-real-ip is set"
    {
      res = fcla_get_d(
        "http://127.0.0.1:4001/files/a/b/hello.txt",
        "X-Real-IP", "127.0.0.1", NULL);

      //printf("res: %i\n", res->status_code);
      //if (res->headers)
      //{
      //  for (flu_node *n = res->headers->first; n; n = n->next)
      //  {
      //    printf("* %s: %s\n", n->key, (char *)n->item);
      //  }
      //}

      expect(res->status_code i== 200);
      expect(res->body === "");

      expect(flu_list_get(res->headers, "x-accel-redirect") === ""
        "../spec/www/a/b/hello.txt");
    }

    it "serves the reason text for 4xx and 5xx responses"
    {
      res = fcla_get("http://127.0.0.1:4001/nemo");

      expect(res->status_code i== 404);
      expect(res->body === "not found");
    }
  }

  describe "shervin and auth"
  {
    before each
    {
      fcla_response *res = NULL;
    }
    after each
    {
      fcla_response_free(res);
    }

    it "doesn't start a session upon failed login"
    {
      res = fcla_post("http://127.0.0.1:4001/login", NULL, "u=toto;p=nada");

      expect(res->status_code i== 401);

      char *s = flu_list_get(res->headers, "set-cookie");
      expect(s == NULL);
    }

    it "starts a session upon sucessful login"
    {
      res = fcla_post("http://127.0.0.1:4001/login", NULL, "u=toto;p=toto");

      expect(res->status_code i== 200);

      //flu_putf(flu_list_to_sm(res->headers));
      char *s = flu_list_get(res->headers, "set-cookie");
      expect(s != NULL);
      expect(s ^== "flon.io.shervin=");
      expect(s >== ";Expires=");
      expect(s >== ";HttpOnly");
      expect(strstr(s, ";Secure") == NULL);
    }

    it "supports a login/play/logout webflow"
    {
      // unauthorized

      res = fcla_get(
        "http://127.0.0.1:4001/secret");

      //flu_putf(fcla_response_to_s(res));
      expect(res->status_code i== 401);

      // login

      fcla_response_free(res);
      res = fcla_post(
        "http://127.0.0.1:4001/login", NULL, "u=toto;p=toto");

      //flu_putf(fcla_response_to_s(res));
      expect(res->status_code i== 200);

      char *cookie0 = rdz_strdup(flu_list_get(res->headers, "set-cookie"));
      *strchr(cookie0, ';') = 0;

      // authorized

      fcla_response_free(res);
      res = fcla_get_d(
        "http://127.0.0.1:4001/secret", "cookie", cookie0, NULL);

      //flu_putf(fcla_response_to_s(res));
      expect(res->status_code i== 200);

      char *cookie1 = rdz_strdup(flu_list_get(res->headers, "set-cookie"));
      *strchr(cookie1, ';') = 0;

      expect(cookie1 !== cookie0);

      expect(res->body === "there are no secrets.");

      // logout

      fcla_response_free(res);
      res = fcla_post_d(
        "http://127.0.0.1:4001/login", "", "cookie", cookie1, NULL);

      flu_putf(fcla_response_to_s(res));
      expect(res->status_code i== 200);

      // unauthorized

      fcla_response_free(res);
      res = fcla_get_d(
        "http://127.0.0.1:4001/secret", "cookie", cookie1, NULL);

      flu_putf(fcla_response_to_s(res));
      expect(res->status_code i== 401);
    }
  }
}

<commit_msg>turn debug off for session auth webflow spec<commit_after>
//
// specifying shervin
//
// Sun Sep 21 15:35:29 JST 2014
//

#include "shervin.h"
#include "shv_protected.h" // direct access to fshv_request methods

#include "servman.h"
#include "flutil.h"
#include "bocla.h"


context "live"
{
  before all
  {
    server_start();
  }
  after all
  {
    server_stop();
    //system("lsof -i :4001");
  }

  describe "shervin"
  {
    before each
    {
      fcla_response *res = NULL;
    }
    after each
    {
      fcla_response_free(res);
    }

    it "serves /hello/toto"
    {
      res = fcla_get("http://127.0.0.1:4001/hello/toto");

      expect(res->status_code i== 200);
      expect(res->body === "hello toto\n");
    }

    it "serves back the body it received in /mirror"
    {
      res = fcla_post("http://127.0.0.1:4001/mirror", NULL, "nada");

      char *body = strstr(res->body, "\r\n\r\n") + 4;

      expect(res->status_code i== 200);
      expect(body === "nada");
    }

    it "serves files"
    {
      res = fcla_get("http://127.0.0.1:4001/files/a/b/hello.txt");

      //printf("res: %i\n", res->status_code);
      //if (res->headers)
      //{
      //  for (flu_node *n = res->headers->first; n; n = n->next)
      //  {
      //    printf("* %s: %s\n", n->key, (char *)n->item);
      //  }
      //}

      expect(res->status_code i== 200);
      expect(res->body === "hello world\n");

      expect(flu_list_get(res->headers, "x-accel-redirect") === ""
        "../spec/www/a/b/hello.txt");
    }

    it "doesn't actually serve the file if x-real-ip is set"
    {
      res = fcla_get_d(
        "http://127.0.0.1:4001/files/a/b/hello.txt",
        "X-Real-IP", "127.0.0.1", NULL);

      //printf("res: %i\n", res->status_code);
      //if (res->headers)
      //{
      //  for (flu_node *n = res->headers->first; n; n = n->next)
      //  {
      //    printf("* %s: %s\n", n->key, (char *)n->item);
      //  }
      //}

      expect(res->status_code i== 200);
      expect(res->body === "");

      expect(flu_list_get(res->headers, "x-accel-redirect") === ""
        "../spec/www/a/b/hello.txt");
    }

    it "serves the reason text for 4xx and 5xx responses"
    {
      res = fcla_get("http://127.0.0.1:4001/nemo");

      expect(res->status_code i== 404);
      expect(res->body === "not found");
    }
  }

  describe "shervin and auth"
  {
    before each
    {
      fcla_response *res = NULL;
    }
    after each
    {
      fcla_response_free(res);
    }

    it "doesn't start a session upon failed login"
    {
      res = fcla_post("http://127.0.0.1:4001/login", NULL, "u=toto;p=nada");

      expect(res->status_code i== 401);

      char *s = flu_list_get(res->headers, "set-cookie");
      expect(s == NULL);
    }

    it "starts a session upon sucessful login"
    {
      res = fcla_post("http://127.0.0.1:4001/login", NULL, "u=toto;p=toto");

      expect(res->status_code i== 200);

      //flu_putf(flu_list_to_sm(res->headers));
      char *s = flu_list_get(res->headers, "set-cookie");
      expect(s != NULL);
      expect(s ^== "flon.io.shervin=");
      expect(s >== ";Expires=");
      expect(s >== ";HttpOnly");
      expect(strstr(s, ";Secure") == NULL);
    }

    it "supports a login/play/logout webflow"
    {
      // unauthorized

      res = fcla_get(
        "http://127.0.0.1:4001/secret");

      //flu_putf(fcla_response_to_s(res));
      expect(res->status_code i== 401);

      // login

      fcla_response_free(res);
      res = fcla_post(
        "http://127.0.0.1:4001/login", NULL, "u=toto;p=toto");

      //flu_putf(fcla_response_to_s(res));
      expect(res->status_code i== 200);

      char *cookie0 = rdz_strdup(flu_list_get(res->headers, "set-cookie"));
      *strchr(cookie0, ';') = 0;

      // authorized

      fcla_response_free(res);
      res = fcla_get_d(
        "http://127.0.0.1:4001/secret", "cookie", cookie0, NULL);

      //flu_putf(fcla_response_to_s(res));
      expect(res->status_code i== 200);

      char *cookie1 = rdz_strdup(flu_list_get(res->headers, "set-cookie"));
      *strchr(cookie1, ';') = 0;

      expect(cookie1 !== cookie0);

      expect(res->body === "there are no secrets.");

      // logout

      fcla_response_free(res);
      res = fcla_post_d(
        "http://127.0.0.1:4001/login", "", "cookie", cookie1, NULL);

      //flu_putf(fcla_response_to_s(res));
      expect(res->status_code i== 200);

      // unauthorized

      fcla_response_free(res);
      res = fcla_get_d(
        "http://127.0.0.1:4001/secret", "cookie", cookie1, NULL);

      //flu_putf(fcla_response_to_s(res));
      expect(res->status_code i== 401);

      free(cookie0); free(cookie1);
    }
  }
}


{'fraction_non_alphanumeric': 0.19201196070055532, 'fraction_numerical': 0.041221700128150365, 'mean_word_length': 2.1929764745993863, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 26, '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': '1152436', 'n_tokens_mistral': 3920, 'n_tokens_neox': 3618, 'n_words': 633}