Пример #1
0
void
test_response_append_body_len(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_append_body_len(res, "heheasd", 4);
    g_assert_cmpstr(res->body->str, ==, "lolhehe");
    balde_response_free(res);
}
Пример #2
0
void
test_response_get_tmpl_var(void)
{
    balde_response_t *res = balde_make_response("lol");
    g_hash_table_insert(res->template_ctx, g_strdup("bola"), g_strdup("guda"));
    g_assert_cmpstr(balde_response_get_tmpl_var(res, "bola"), ==, "guda");
    balde_response_free(res);
}
Пример #3
0
void
test_response_set_cookie_with_secure(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_set_cookie(res, "bola", "guda", -1, -1, NULL, NULL, TRUE);
    GSList *tmp = g_hash_table_lookup(res->headers, "set-cookie");
    g_assert_cmpstr(tmp->data, ==, "bola=\"guda\"; Secure; Path=/");
    balde_response_free(res);
}
Пример #4
0
void
test_response_set_cookie_with_domain(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_set_cookie(res, "bola", "guda", -1, -1, NULL, "bola.com", FALSE);
    GSList *tmp = g_hash_table_lookup(res->headers, "set-cookie");
    g_assert_cmpstr(tmp->data, ==, "bola=\"guda\"; Domain=\"bola.com\"; Path=/");
    balde_response_free(res);
}
Пример #5
0
void
test_response_set_tmpl_var(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_set_tmpl_var(res, "bola", "guda");
    g_assert(g_hash_table_size(res->template_ctx) == 1);
    g_assert_cmpstr(g_hash_table_lookup(res->template_ctx, "bola"), ==, "guda");
    balde_response_free(res);
}
Пример #6
0
void
test_response_delete_cookie(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_delete_cookie(res, "bola", NULL, NULL);
    GSList *tmp = g_hash_table_lookup(res->headers, "set-cookie");
    g_assert_cmpstr(tmp->data, ==,
        "bola=\"\"; Expires=Thu, 01-Jan-1970 00:00:00 GMT; Max-Age=0; Path=/");
    balde_response_free(res);
}
Пример #7
0
void
test_response_set_cookie_with_expires_and_max_age(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_set_cookie(res, "bola", "guda", 60, 1235555555, NULL, NULL, FALSE);
    GSList *tmp = g_hash_table_lookup(res->headers, "set-cookie");
    g_assert_cmpstr(tmp->data, ==,
        "bola=\"guda\"; Expires=Wed, 25-Feb-2009 09:52:35 GMT; Max-Age=60; Path=/");
    balde_response_free(res);
}
Пример #8
0
void
test_response_render_without_body(void)
{
    balde_response_t *res = balde_make_response("lol");
    GString *out = balde_response_render(res, FALSE);
    g_assert_cmpstr(out->str, ==,
        "Content-Type: text/html; charset=utf-8\r\nContent-Length: 3\r\n\r\n");
    g_string_free(out, TRUE);
    balde_response_free(res);
}
Пример #9
0
void
test_response_set_headers(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_set_header(res, "AsDf-QwEr", "test");
    g_assert(g_hash_table_size(res->headers) == 1);
    GSList *tmp = g_hash_table_lookup(res->headers, "asdf-qwer");
    g_assert_cmpstr(tmp->data, ==, "test");
    balde_response_free(res);
}
Пример #10
0
void
test_response_set_cookie_with_expires(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_set_cookie(res, "bola", "guda", -1, 1234567890, NULL, NULL,
        FALSE);
    GSList *tmp = g_hash_table_lookup(res->headers, "set-cookie");
    g_assert_cmpstr(tmp->data, ==,
        "bola=\"guda\"; Expires=Fri, 13-Feb-2009 23:31:30 GMT; Path=/");
    balde_response_free(res);
}
Пример #11
0
void
test_response_render_with_custom_mime_type(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_set_header(res, "content-type", "text/plain");
    GString *out = balde_response_render(res, TRUE);
    g_assert_cmpstr(out->str, ==,
        "Content-Type: text/plain\r\nContent-Length: 3\r\n\r\nlol");
    g_string_free(out, TRUE);
    balde_response_free(res);
}
Пример #12
0
void
test_make_response_len(void)
{
    balde_response_t *res = balde_make_response_len("lolasdf", 3);
    g_assert(res != NULL);
    g_assert(res->status_code == 200);
    g_assert(g_hash_table_size(res->headers) == 0);
    g_assert(g_hash_table_size(res->template_ctx) == 0);
    g_assert_cmpstr(res->body->str, ==, "lol");
    balde_response_free(res);
}
Пример #13
0
void
test_make_response_from_exception(void)
{
    balde_app_t *app = balde_app_init();
    balde_abort_set_error(app, 404);
    balde_response_t *res = balde_make_response_from_exception(app->error);
    g_assert(res != NULL);
    g_assert(res->status_code == 404);
    g_assert(g_hash_table_size(res->headers) == 1);
    GSList *tmp = g_hash_table_lookup(res->headers, "content-type");
    g_assert_cmpstr(tmp->data, ==, "text/plain; charset=utf-8");
    g_assert_cmpstr(res->body->str, ==,
        "404 Not Found\n\nThe requested URL was not found on the server. "
        "If you entered the URL manually please check your spelling and try again.\n");
    balde_response_free(res);
    balde_app_free(app);
}
Пример #14
0
void
test_response_render_exception_without_body(void)
{
    balde_app_t *app = balde_app_init();
    balde_abort_set_error(app, 404);
    balde_response_t *res = balde_make_response_from_exception(app->error);
    g_assert(res != NULL);
    GString *out = balde_response_render(res, FALSE);
    g_assert_cmpstr(out->str, ==,
        "Status: 404 Not Found\r\n"
        "Content-Type: text/plain; charset=utf-8\r\n"
        "Content-Length: 136\r\n"
        "\r\n");
    g_string_free(out, TRUE);
    balde_response_free(res);
    balde_app_free(app);
}
Пример #15
0
void
test_make_response_from_external_exception(void)
{
    balde_app_t *app = balde_app_init();
    balde_abort_set_error(app, 1024);
    balde_response_t *res = balde_make_response_from_exception(app->error);
    g_assert(res != NULL);
    g_assert(res->status_code == 500);
    g_assert(g_hash_table_size(res->headers) == 1);
    GSList *tmp = g_hash_table_lookup(res->headers, "content-type");
    g_assert_cmpstr(tmp->data, ==, "text/plain; charset=utf-8");
    g_assert_cmpstr(res->body->str, ==,
        "500 Internal Server Error\n\nThe server encountered an internal error "
        "and was unable to complete your request. Either the server is "
        "overloaded or there is an error in the application.\n\n(null)\n");
    balde_response_free(res);
    balde_app_free(app);
}
Пример #16
0
void
test_response_render_exception(void)
{
    balde_app_t *app = balde_app_init();
    balde_abort_set_error(app, 404);
    balde_response_t *res = balde_make_response_from_exception(app->error);
    g_assert(res != NULL);
    GString *out = balde_response_render(res, TRUE);
    g_assert_cmpstr(out->str, ==,
        "Status: 404 Not Found\r\n"
        "Content-Type: text/plain; charset=utf-8\r\n"
        "Content-Length: 136\r\n"
        "\r\n"
        "404 Not Found\n\nThe requested URL was not found on the server. "
        "If you entered the URL manually please check your spelling and try again.\n");
    g_string_free(out, TRUE);
    balde_response_free(res);
    balde_app_free(app);
}
Пример #17
0
void
test_response_render_with_multiple_cookies(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_set_cookie(res, "bola", "guda", 60, -1, NULL, NULL, FALSE);
    balde_response_set_cookie(res, "asd", "qwe", -1, -1, NULL, NULL, FALSE);
    balde_response_set_cookie(res, "xd", ":D", -1, -1, "/bola/", NULL, FALSE);
    GString *out = balde_response_render(res, TRUE);
    g_assert_cmpstr(out->str, ==,
        "Set-Cookie: bola=\"guda\"; Expires=Fri, 13-Feb-2009 23:32:30 GMT; Max-Age=60; Path=/\r\n"
        "Set-Cookie: asd=\"qwe\"; Path=/\r\n"
        "Set-Cookie: xd=\":D\"; Path=/bola/\r\n"
        "Content-Type: text/html; charset=utf-8\r\n"
        "Content-Length: 3\r\n"
        "\r\n"
        "lol");
    g_string_free(out, TRUE);
    balde_response_free(res);
}
Пример #18
0
Файл: app.c Проект: GnLWeB/balde
GString*
balde_app_main_loop(balde_app_t *app, balde_request_env_t *env,
    balde_response_render_t render, balde_http_exception_code_t *status_code)
{
    balde_request_t *request = NULL;
    balde_response_t *response = NULL;
    balde_response_t *error_response = NULL;
    gchar *endpoint = NULL;
    gboolean with_body = TRUE;
    GString *rv = NULL;

    // render startup error, if any
    if (app->error != NULL) {
        error_response = balde_make_response_from_exception(app->error);
        rv = render(error_response, with_body);
        if (status_code != NULL)
            *status_code = error_response->status_code;
        balde_response_free(error_response);

        // free env, because it should be free'd by main loop and will not be
        // used anymore.
        balde_request_env_free(env);

        return rv;
    }

    request = balde_make_request(app, env);

    with_body = ! (request->method & BALDE_HTTP_HEAD);

    for (GSList *tmp = app->priv->before_requests; tmp != NULL; tmp = g_slist_next(tmp)) {
        balde_before_request_t *hook = tmp->data;
        hook->before_request_func(app, request);

        if (app->error != NULL) {
            error_response = balde_make_response_from_exception(app->error);
            rv = render(error_response, with_body);
            if (status_code != NULL)
                *status_code = error_response->status_code;
            balde_response_free(error_response);
            g_clear_error(&app->error);
            return rv;
        }
    }

    balde_app_t *app_copy = balde_app_copy(app);

    // get the view
    endpoint = balde_dispatch_from_path(app_copy->priv->views, request->path,
        &(request->priv->view_args));
    if (endpoint == NULL) {  // no view found! :(
        balde_abort_set_error(app_copy, 404);
    }
    else {
        // validate http method
        balde_view_t *view = balde_app_get_view_from_endpoint(app_copy, endpoint);
        if (request->method & view->url_rule->method) {
            // answer OPTIONS automatically
            if (request->method == BALDE_HTTP_OPTIONS) {
                response = balde_make_response("");
                gchar *allow = balde_list_allowed_methods(view->url_rule->method);
                balde_response_set_header(response, "Allow", allow);
                g_free(allow);
            }
            // run the view
            else {
                response = view->view_func(app_copy, request);
            }
        }
        // method not allowed
        else {
            balde_abort_set_error(app_copy, 405);
        }
        g_free(endpoint);
    }

    balde_request_free(request);

    if (app_copy->error != NULL) {
        error_response = balde_make_response_from_exception(app_copy->error);
        rv = render(error_response, with_body);
        if (status_code != NULL)
            *status_code = error_response->status_code;
        balde_response_free(response);
        balde_response_free(error_response);
        balde_app_free(app_copy);
        return rv;
    }

    rv = render(response, with_body);
    if (status_code != NULL)
        *status_code = response->status_code;
    balde_response_free(response);
    balde_app_free(app_copy);

    return rv;
}