Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
whayter committed Nov 1, 2021
1 parent b810dce commit 3e03b40
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 95 deletions.
74 changes: 38 additions & 36 deletions __tests__/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const frisby = require('frisby');

describe('webserv', function() {

var baseUri = 'http://localhost:80/';
var baseUri = 'http://localhost:8080/';

// FileReader(""); read index.php and compare content to see if it match response body

Expand All @@ -15,14 +15,14 @@ it ('GET /favicon.ico', function () {
.expect('status', 200)
});

it ('GET localhost:80/', function () {
it ('GET localhost:8080/', function () {
return frisby
.get(baseUri)
.expect('status', 200)
.expectNot('bodyContains', '<?php')
});

it ('GET localhost:80/index.php', function () {
it ('GET localhost:8080/index.php', function () {
return frisby
.get(baseUri + 'index.php')
.expect('status', 200)
Expand All @@ -33,7 +33,7 @@ it ('GET localhost:80/index.php', function () {
.expectNot('bodyContains', '<?php')
});

it ('GET localhost:80/cgiParams.php?query=working', function () {
it ('GET localhost:8080/cgiParams.php?query=working', function () {
return frisby
.get(baseUri + 'cgiParams.php?query=working')
.expect('status', 200)
Expand All @@ -46,7 +46,7 @@ it ('GET localhost:80/cgiParams.php?query=working', function () {
.expect('bodyContains', 'query=working')
});

it ('GET localhost:80/cgiParams.php?webserv=done', function () {
it ('GET localhost:8080/cgiParams.php?webserv=done', function () {
return frisby
.get(baseUri + 'cgiParams.php?webserv=done')
.expect('status', 200)
Expand All @@ -59,38 +59,38 @@ it ('GET localhost:80/cgiParams.php?webserv=done', function () {
.expect('bodyContains', 'webserv=done')
});

it ('GET localhost:80/subject.html', function () {
it ('GET localhost:8080/subject.html', function () {
return frisby
.get(baseUri + '/subject.html')
.expect('status', 200)
.expect('header', 'content-type', "text/html")
.expect('bodyContains', 'Mandatory part')
});

it ('GET localhost:80/NotExist', function () {
it ('GET localhost:8080/NotExist', function () {
return frisby
.get(baseUri + '/NotExist')
.expect('status', 404)
.expect('bodyContains', 'Seems like your page doesn\'t exist anymore')
});

it ('GET localhost:80/emptyFile.html', function () {
it ('GET localhost:8080/emptyFile.html', function () {
return frisby
.get(baseUri + '/emptyFile.html')
.expect('status', 200)
.expect('header', 'content-type', "text/html")
.expect('header', 'content-length', "0")
});

it ('GET localhost:80/emptyFile.php', function () {
it ('GET localhost:8080/emptyFile.php', function () {
return frisby
.get(baseUri + '/emptyFile.php')
.expect('status', 200)
.expect('header', 'content-type', "text/html; charset=UTF-8")
.expect('header', 'content-length', "0")
});

it ('GET localhost:80/autoindex', function () {
it ('GET localhost:8080/autoindex', function () {
return frisby
.get(baseUri + 'autoindex')
.expect('status', 200)
Expand All @@ -99,44 +99,44 @@ it ('GET localhost:80/autoindex', function () {
.expect('bodyContains', 'teletubbies')
});

it ('GET localhost:80/bad_cgi', function () {
it ('GET localhost:8080/bad_cgi', function () {
return frisby
.get(baseUri + 'bad_cgi')
.expect('status', 500)
});

it ('GET localhost:81', function () {
it ('GET localhost:8081', function () {
return frisby
.get('http://localhost:81')
.get('http://localhost:8081')
.expect('status', 200)
.expect('bodyContains', '<?php')
});

it ('GET localhost:82', function () {
it ('GET localhost:8082', function () {
return frisby
.get('http://localhost:82/')
.get('http://localhost:8082/')
.expect('status', 200)
.expect('bodyContains', 'This project is here to make you write your HTTP server.')
});

it ('GET localhost:82/text', function () {
it ('GET localhost:8082/text', function () {
return frisby
.get('http://localhost:82/text')
.get('http://localhost:8082/text')
.expect('status', 204)
.expectNot('header', 'Content-Length')
.expectNot('bodyContains', 'this is a return directive with text')
});

it ('GET localhost:82/uri', function () {
it ('GET localhost:8082/uri', function () {
return frisby
.get('http://localhost:82/uri')
.get('http://localhost:8082/uri')
.expect('status', 200)
.expect('bodyContains', 'Copyright The Closure Library Authors')
});

it ('GET localhost:83', function () {
it ('GET localhost:8083', function () {
return frisby
.get('http://localhost:83')
.get('http://localhost:8083')
.expect('status', 200)
.expect('bodyContains', 'index of /')
.expect('bodyContains', 'Makefile')
Expand All @@ -145,40 +145,42 @@ it ('GET localhost:83', function () {
//////////////////////////////////////////////////////////////////////////////
/// POST ////////////////////////////////////////////////////////////////////

it ('POST localhost:80/ with body size bigger than max_body_size', function () {
let oneM = 1024 * 1024

it ('POST localhost:8080/ with body size bigger than max_body_size', function () {
return frisby
.setup({
request: {
body: "x".repeat(1025)
body: "x".repeat(oneM + 1)
}
})
.post(baseUri + 'test.html')
.expect('status', 413)
});

it ('POST localhost:80/ with body size way bigger than max_body_size', function () {
it ('POST localhost:8080/ with body size way bigger than max_body_size', function () {
return frisby
.setup({
request: {
body: "x".repeat(102500)
body: "x".repeat(oneM * 2)
}
})
.post(baseUri + 'test.html')
.expect('status', 413)
});

it ('POST localhost:80/ with body size = max_body_size', function () {
it ('POST localhost:8080/ with body size = max_body_size', function () {
return frisby
.setup({
request: {
body: "x".repeat(1024)
body: "x".repeat(oneM)
}
})
.post(baseUri + 'testPostMethod.html')
.expect('status', 201)
});

it ('POST localhost:80/cgiParams.php', function () {
it ('POST localhost:8080/cgiParams.php', function () {
return frisby
.setup({
request: {
Expand All @@ -193,31 +195,31 @@ it ('POST localhost:80/cgiParams.php', function () {
//////////////////////////////////////////////////////////////////////////////
/// DELETE //////////////////////////////////////////////////////////////////

it ('DELETE http://localhost:80/testPostMethod.html', function () {
it ('DELETE http://localhost:8080/testPostMethod.html', function () {
return frisby
.del('http://localhost:80/testPostMethod.html')
.del('http://localhost:8080/testPostMethod.html')
.expect('status', 405);
});

it ('DELETE http://localhost:84/testPostMethod.html', function () {
it ('DELETE http://localhost:8084/testPostMethod.html', function () {
return frisby
.del('http://localhost:84/testPostMethod.html')
.del('http://localhost:8084/testPostMethod.html')
.expect('status', 200)
.expect('bodyContains', 'File deleted')
});

it ('DELETE http://localhost:84/notExist.html', function () {
it ('DELETE http://localhost:8084/notExist.html', function () {
return frisby
.del('http://localhost:84/notExist.html')
.del('http://localhost:8084/notExist.html')
.expect('status', 404)
});

//////////////////////////////////////////////////////////////////////////////
/// PUT /////////////////////////////////////////////////////////////////////

it ('PUT http://localhost:80 should return Not implemented 501', function () {
it ('PUT http://localhost:8080 should return Not implemented 501', function () {
return frisby
.put('http://localhost:80')
.put('http://localhost:8080')
.expect('status', 501);
});

Expand Down
2 changes: 1 addition & 1 deletion config/macos_webserv.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ server {
server_name srv_one;
error_page 404 ./404.html;
client_max_body_size 1M;
upload_store ./www/upload;
upload_store ./www/uploads;

location / {
limit_except GET POST;
Expand Down
2 changes: 1 addition & 1 deletion config/ubuntu_webserv.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ server {
server_name srv_one;
error_page 404 ./404.html;
client_max_body_size 1M;
upload_store ./www/upload;
upload_store ./www/uploads;

location / {
limit_except GET POST;
Expand Down
4 changes: 3 additions & 1 deletion includes/ft/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: hwinston <hwinston@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/16 16:06:58 by juligonz #+# #+# */
/* Updated: 2021/09/18 11:59:28 by hwinston ### ########.fr */
/* Updated: 2021/11/01 14:46:32 by hwinston ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -21,6 +21,8 @@

namespace ft {

void writeContentToFile(const filesystem::path& path, const char *content, size_t n);
std::vector<unsigned char> getFileContent(const filesystem::path& path);
std::string intToString(int i);
std::string stringifyVector(std::vector<unsigned char> v);
std::vector<unsigned char> vectorizeString(std::string s);
Expand Down
2 changes: 1 addition & 1 deletion includes/http/messageBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ Response redirectResponse(const Context& ctxt, Response& response);
Response autoIndexResponse(const Context& ctxt, Request& request, Response& response);
Response errorResponse(const Context& ctxt, Response& response, Status error);

Response postMultipart(const Context& ctxt, Request& request, Response& response);
int postContent(std::string path, content_type content);
std::vector<unsigned char> getFileContent(const ft::filesystem::path& path);

template <class Message>
std::string stringifyMessage(Message& message)
Expand Down
24 changes: 21 additions & 3 deletions src/ft/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,36 @@
/* By: hwinston <hwinston@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/16 16:10:02 by juligonz #+# #+# */
/* Updated: 2021/09/18 11:59:12 by hwinston ### ########.fr */
/* Updated: 2021/11/01 14:46:25 by hwinston ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft/utility.hpp"
#include <algorithm>
#include "utility.hpp"

#include <algorithm>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fstream>

namespace ft {

void writeContentToFile(const filesystem::path& path, const char *content, size_t n)
{
std::ofstream file;
file.open(path.c_str(), std::ofstream::binary);
file.write(content, n);
file.close();
}

std::vector<unsigned char> getFileContent(const filesystem::path& path)
{
std::ifstream file;
file.open(path.c_str(), std::ifstream::in);
std::vector<unsigned char> buffer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
return buffer;
}

std::string intToString(int i)
{
std::string result;
Expand Down
Loading

0 comments on commit 3e03b40

Please sign in to comment.